1###############
2LPCXpresso55S69
3###############
4
5****************
61. Building TF-M
7****************
8
9There are two options for the TF-M build - with or without secondary bootloader (BL2).
10
111.1 Building TF-M demo without BL2
12==================================
13To build S and NS application image for the LPCXpresso55S69, run the ``build_tfm_demo.py`` script in ``platform/ext/target/nxp/lpcxpresso55s69/scripts``
14
15Or do it manually using the following commands:
16
17.. code:: bash
18
19    $ cmake -S . -B build -DTFM_PLATFORM=nxp/lpcxpresso55s69 -DTFM_TOOLCHAIN_FILE=toolchain_GNUARM.cmake -DTFM_PROFILE=profile_medium -DCMAKE_BUILD_TYPE=Relwithdebinfo -DBL2=OFF -DTFM_ISOLATION_LEVEL=2 -G"Unix Makefiles"
20    $ cd build && make install
21
221.2 Building TF-M demo with BL2
23===============================
24
25To build S and NS application along with a BL2 (bootloader) image for the
26LPCXpresso55S69 run the ``build_tfm_demo_bl2.py`` script in ``platform/ext/target/nxp/lpcxpresso55s69/scripts``
27
28Or do it manually using the following commands:
29
30.. code:: bash
31
32    $ cmake -S . -B build -DTFM_PLATFORM=nxp/lpcxpresso55s69 -DTFM_TOOLCHAIN_FILE=toolchain_GNUARM.cmake -DTFM_PROFILE=profile_medium -DCMAKE_BUILD_TYPE=Relwithdebinfo -DTFM_ISOLATION_LEVEL=2 -G"Unix Makefiles"
33    $ cd build && make install
34
351.3 Building TF-M regression tests
36==================================
37
38To run the S and NS regression tests (``TEST_S=ON`` and ``TEST_NS=ON``), the
39secondary image areas must be set to 0 (firmware updates are not possible).
40Use the ``build_tfm_regression.py`` script in ``platform/ext/target/nxp/lpcxpresso55s69/scripts``
41or do it manually using following commands:
42
43.. code:: bash
44
45    $ cmake -S . -B build -DTFM_PLATFORM=nxp/lpcxpresso55s69 -DTFM_TOOLCHAIN_FILE=toolchain_GNUARM.cmake -DTFM_PROFILE=profile_medium -DCMAKE_BUILD_TYPE=Relwithdebinfo -DBL2=OFF -DTEST_S=ON -DTEST_NS=ON -DTFM_ISOLATION_LEVEL=2 -G"Unix Makefiles"
46    $ cd build && make install
47
48.. Note::
49
50    Currently ``Debug`` cannot be selected as build type and regression tests
51    cannot be run on the board without modifying the flash layout due to the
52    amount of available on-chip flash memory.
53
54****************
552. Flashing TF-M
56****************
57
58After generating the binaries, there are three options to flash them using:
59    1) **External Segger J-Link flasher**
60    2) **On-board J-Link debugger** - with update of LPC-Link2 debugger to the Segger J-Link firmware
61    3) **PyOCD** - supports both DAPLink and J-Link interfaces. The LPCXpresso55S69 boards, by default, use DAPLink firmware.
62
632.1 Flashing with Segger J-Link
64===============================
65
66For command line flashing it is recommended to use external Segger J-Link flasher or to
67update the LPC-Link 2 debugger on the development board with the firmware provided by Segger,
68which makes the device behave as if there is an on-board J-Link debugger.
69
70For onboard J-Link debugger option please follow this step:
71
722.1.1 Update the LPC-Link 2 to Segger J-Link
73--------------------------------------------
74
75-  Install a recent version of the `J-Link Software and Documentation
76   Pack <https://www.segger.com/downloads/jlink#J-LinkSoftwareAndDocumentationPack>`__.
77   Version **6.98b** was used with this guide.
78
79-  Update the on-board LPC-Link 2 to use the latest J-Link firmware,
80   following the instructions from Segger: `Getting Started with
81   LPC-Link2 <https://www.segger.com/products/debug-probes/j-link/models/other-j-links/lpc-link-2/>`__.
82-  Link the DFU jumper (J4) and make power cycle
83-  Flash the **NXP LPCXpresso On-Board** firmware image with ``lpcscrypt`` from the ``lpcscrypt_2.1.2_57/scripts`` folder as follows:
84
85.. tabs::
86
87    .. group-tab:: Linux
88
89        .. code-block:: bash
90
91            $ ./program_JLINK ../probe_firmware/LPCXpressoV2/Firmware_JLink_LPCXpressoV2_20190404.bin
92
93    .. group-tab:: Windows
94
95        .. code-block:: bash
96
97            $ program_JLINK ../probe_firmware/LPCXpressoV2/Firmware_JLink_LPCXpressoV2_20190404.bin
98
99Then remove the link on the DFU jumper and power cycle.
100
1012.1.2 Flash images with ``JLinkCommander``
102------------------------------------------
103
104To flash TF-M images use the flash scripts provided in ``platform/ext/target/nxp/lpcxpresso55s69/scripts`` folder:
105    - ``flash_JLink.py`` - for uploading image without BL2
106    - ``flash_bl2_JLink.py`` - for uploading image with BL2
107
108Or you can do it manually according to paragraph ``2.1.2.1``.
109
1102.1.2.1 Connect to the board using ``JLinkCommander``
111^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112
113.. tabs::
114
115    .. group-tab:: Linux
116
117        .. code-block:: bash
118
119            $ JLinkExe -device lpc55s69 -if swd -speed 2000 -autoconnect 1
120
121            SEGGER J-Link Commander V6.98b (Compiled Mar 12 2021 15:03:29)
122            DLL version V6.98b, compiled Mar 12 2021 15:02:22
123
124            Connecting to J-Link via USB...O.K.
125            Firmware: J-Link LPCXpresso V2 compiled Apr  4 2019 16:54:03
126            Hardware version: V1.00
127            S/N: 729458359
128            VTref=3.300V
129            Device "LPC55S69_M33_0" selected.
130            ...
131            Cortex-M33 identified.
132
133    .. group-tab:: Windows
134
135        .. code-block:: bash
136
137            $ JLink -device lpc55s69 -if swd -speed 2000 -autoconnect 1
138
139            SEGGER J-Link Commander V6.98b (Compiled Mar 12 2021 15:03:29)
140            DLL version V6.98b, compiled Mar 12 2021 15:02:22
141
142            Connecting to J-Link via USB...O.K.
143            Firmware: J-Link LPCXpresso V2 compiled Apr  4 2019 16:54:03
144            Hardware version: V1.00
145            S/N: 729458359
146            VTref=3.300V
147            Device "LPC55S69_M33_0" selected.
148            ...
149            Cortex-M33 identified.
150
1512.1.2.2 Flash the built images
152^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
153
154When BL2 is disabled, flash the generated hex secure and non-secure images:
155::
156
157    J-Link> loadfile ${BUILD_DIR}/bl2.hex
158    J-Link> loadfile ${BUILD_DIR}/tfm_s_signed.bin 0x00008000
159    J-Link> loadfile ${BUILD_DIR}/tfm_ns_signed.bin 0x00030000
160
161If you built TF-M with the BL2 secondary bootloader, use following commands:
162::
163
164    J-Link> loadfile ${BUILD_DIR}/tfm_s.hex
165    J-Link> loadfile ${BUILD_DIR}/tfm_ns.hex
166
1672.2 Flashing with PyOCD
168=======================
169PyOCD is an open source Python package for programming and debugging Arm Cortex-M microcontrollers using multiple supported types of USB debug probes.
170See: `PyOCD <https://pypi.org/project/pyocd/>`__
171
172To flash TF-M images with PyOCD you can use the flash scripts provided in ``platform/ext/target/nxp/lpcxpresso55s69/scripts`` folder:
173    - ``flash_PyOCD.py`` - for uploading image without BL2
174    - ``flash_bl2_PyOCD.py`` - for uploading image with BL2
175
176You should get the following output (flashing without BL2):
177::
178
179    $ python flash_PyOCD.py
180    0001749:INFO:eraser:Mass erasing device...
181    0001749:INFO:eraser:Erasing chip...
182    0001902:INFO:eraser:Done
183    0001902:INFO:eraser:Successfully erased.
184    [====================] 100%
185    0007694:INFO:loader:Erased 262144 bytes (8 sectors), programmed 203776 bytes (398 pages), skipped 0 bytes (0 pages) at 33.91 kB/s
186    [====================] 100%
187    0005187:INFO:loader:Erased 131072 bytes (4 sectors), programmed 121856 bytes (238 pages), skipped 0 bytes (0 pages) at 34.13 kB/s
188
189
190Or do it manually according the following steps:
191
192If you built TF-M with the BL2 secondary bootloader, use the following commands:
193::
194
195    $ pyocd erase --mass -t LPC55S69
196    $ pyocd flash ${BUILD_DIR}/tfm_s.hex -t LPC55S69
197    $ pyocd flash ${BUILD_DIR}/tfm_ns.hex -t LPC55S69
198
199When BL2 is disabled, flash the generated hex secure and non-secure images:
200::
201
202    $ pyocd erase --mass -t LPC55S69
203    $ pyocd flash ${BUILD_DIR}/bl2.hex -t LPC55S69
204    $ pyocd flash ${BUILD_DIR}/tfm_s_signed.bin --base-address 0x8000 -t LPC55S69
205    $ pyocd flash ${BUILD_DIR}/tfm_ns_signed.bin --base-address 0x30000 -t LPC55S69
206
207.. Note::
208
209    At present, the reset target command does not seem to respond, so you can reset the device to start firmware execution via the physical RESET button (S4). There is sometimes also a stability issue with the flash erasing, so if the script freezes, it is needed to terminate the script, physically reset the target an rerun it again.
210
211.. Warning::
212
213    When using PyOCD on Windows, there might currently occur an issue with the ``libusb`` library. In that case, download the ``libusb`` library from `here <https://libusb.info/>`__ and copy .DLL file into the Python installation folder (next to python.exe)
214
215************
2163. Debugging
217************
218
2193.1 Debugging with Segger Ozone
220===============================
221
222If you have a commercially licensed Segger J-Link, or if you meet the
223license terms for it's use, `Segger's cross-platform Ozone
224tool <https://www.segger.com/products/development-tools/ozone-j-link-debugger/>`__
225can be used to debug TF-M firmware images.
226
227To debug, flash the BL2, S and NS firmware images using the ``flash.py``
228script or command-line options described earlier in this guide, and
229configure a new project on Ozone as follows:
230
231-  Device: LPC55S69
232-  Target Interface: SWD
233-  Target Interface Speed: 2 MHz
234-  Host Interface: USB
235-  Program File: build/secure\_fw/tfm\_s.axf (etc.)
236
237Once the project has been set up, and the firmware has previously been
238flashed to the board, connect to the target via:
239
240-  Debug > Start Debug Session > Attach to a Running Program
241
242At this point, you can set a breakpoint somewhere in the code, such as
243in ``startup_LPC55S69_cm33_core0.s`` at the start of the
244``Reset_Handler``, or near a line like ``bl    SystemInit``, or at
245another appropriate location, and reset the device to debug.
246
2473.2 Debugging with GDB
248======================
249
250    **NOTE**: If you are debugging, make sure to set the
251    build type variable to ``-DCMAKE_BUILD_TYPE=Debug`` when
252    building TF-M so that debug information is available to GDB.
253
254    **NOTE**: When debugging with the mbed-crypto library, it is needed to add an
255    additional ``-DMBEDCRYPTO_BUILD_TYPE=DEBUG`` compile-time switch.
256
257
2583.2.1 Start the GDB server, pointing to the secure application image:
259---------------------------------------------------------------------
260You can use JLinkGDBServer or PyOCD server depending on the interface configured in the previous step.
261
262.. tabs::
263
264    .. group-tab:: J-Link GDB server
265
266        .. code-block:: bash
267
268            $ JLinkGDBServer -device lpc55s69 -if swd -speed 2000
269
270    .. group-tab:: PyOCD GDB server
271
272        .. code:: bash
273
274            $ pyocd gdbserver -f 2000k -t LPC55S69
275
2763.2.2 Connecting to the GDB server
277----------------------------------
278
279In a separate terminal, start the GDB client in ``tui`` (text UI) mode:
280
281.. code:: bash
282
283    $ arm-none-eabi-gdb --tui secure_fw/tfm_s.axf
284
285Then from the client connect to the remote GDB server that was started
286earlier:
287
288.. tabs::
289
290    .. group-tab:: J-Link GDB server
291
292        With ``JLinkGDBServer`` (default port 2331):
293
294        .. code:: bash
295
296            (gdb) target remote:2331
297            Remote debugging using :2331
298
299    .. group-tab:: PyOCD GDB server
300
301        With ``pyocd gdbserver`` (default port 3333):
302
303        .. code:: bash
304
305            (gdb) target remote:3333
306            Remote debugging using :3333
307
308
3093.2.3 Reset and stop at ``main``
310--------------------------------
311
312Set a breakpoint at ``main()`` (found in ``tfm_core.c``), reset the
313device (``monitor reset``), and continue (``c``) execution.
314
315::
316
317    (gdb) break main
318    Breakpoint 1 at 0x10024220: file [path]/secure_fw/core/tfm_core.c, line 189.
319    (gdb) monitor reset
320    (gdb) c
321    Continuing.
322    Note: automatically using hardware breakpoints for read-only addresses.
323
324    Breakpoint 1, main ()
325        at [path]/secure_fw/core/tfm_core.c:189
326    189     tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK,
327
3283.2.4 Commonly used GDB commands
329--------------------------------
330
331You can start, step through, and analyse the code using some of the
332following GDB commands:
333
334+-------------------+---------------------------------------------------------+
335| GDB Command       | Description                                             |
336+===================+=========================================================+
337| ``next``          | Execute the next statement in the program               |
338+-------------------+---------------------------------------------------------+
339| ``step``          | Step until new source line, entering called functions   |
340+-------------------+---------------------------------------------------------+
341| ``until <n>``     | Run until source line ``n`` in the current file         |
342+-------------------+---------------------------------------------------------+
343| ``info locals``   | Display the local variables and their current values    |
344+-------------------+---------------------------------------------------------+
345| ``bt``            | Display a stack backtrace up to the current function    |
346+-------------------+---------------------------------------------------------+
347| ``print <x>``     | Print the expression (ex. ``print my_var``)             |
348+-------------------+---------------------------------------------------------+
349| ``x``             | Examine memory (ex. ``x/s *my_string``)                 |
350+-------------------+---------------------------------------------------------+
351
352From here, you should consult a tutorial or book on GDB to know how to debug
353common problems.
354
355--------------
356
357*Copyright (c) 2021, NXP Semiconductors. All rights reserved.*
358*Copyright (c) 2020, Linaro. All rights reserved.*
359*Copyright (c) 2020-2021, Arm Limited. All rights reserved.*
360*SPDX-License-Identifier: BSD-3-Clause*