Embedded Development Board Learning


Visual Studio Code for ARM with CMake #4 J-Link

Fecha: febrero 6, 2023

Autor: Guillermo Garcia

Categorías: Visual Studio Code Etiquetas:

Visual Studio Code como entorno de desarrollo para proyecto Embedded Systems

En el artículo pasado Visual Studio Code for ARM with CMake #3 Toolchain CMake configuramos el entorno de compilación CMake. Para obtener un archivo ejecutable listo para descargar en nuestra placa NUCLEO-G071RB. En este articulo vamos a configurar las herramientas J-Link para la descarga de código.

Convertir ST-Link a J-Link

Nuestra placa NUCLEO-G071RB cuenta con un depurador integrado en la tarjeta de desarrollo este depurador es especializado para las herramientas de Software de STM32. Para obtener más portabilidad vamos a utilizar las herramientas de Software de SEGGER.

El depurador ST-Link V2 que integra nuestro hardware se puede transformar en un J-Link para lograr esto solo es necesario cambiar el firmware del depurador.

ST Link Reflash Utility

Para hacer el cambio de firmware en nuestro depurador ST-Link emplearemos una herramienta de SEGGER llamada ST-Link Reflash Utility.

Proceso de compilación CMake

Descargamos un archivo ejecutable STLinkReflash.exe con un Script que nos permite instalar el firmware necesario para la conversión.

(c) 2016 SEGGER Microcontroller GmbH & Co. KG    www.segger.com
STLinkReflash compiled Aug 12 2019 10:30:05

The following terms come from SEGGER Microcontroller GmbH & Co. KG ("SEGGER")
You must agree to them in order to proceed.

=============================================================================
* IMPORTANT *
This utility enables You to replace the firmware of an existing ST-LINK
on-board with firmware from SEGGER that makes the ST-LINK on-board
behave J-Link compatible. You do this replacement at Your own risk.
Though extremely unlikely, You are aware that the replacement process may
result in an unusable ST-LINK on-board. The utility and firmware from SEGGER
are provided on an as-is basis and come without any warranty
and without support.
You further agree to only use the firmware provided by SEGGER via this utility,
within the bounds of the license stated on the download page:
https://www.segger.com/jlink-st-link.html
Except as expressly set forth in this agreement,
the Agreement remains unchanged and continues in full force and effect.

=============================================================================

I hereby accept the terms provided by SEGGER.
(A)ccept / (D)ecline
Selection>A

Conectamos la placa seleccionamos la opción 1, para que se realice el cambio de firmware.

At the request of STMICROELECTRONICS ("ST"), the below
"Amendment to Evaluation Board License Agreement" has been added
to this utility. You must agree to this amendment in order to proceed.

=============================================================================
* IMPORTANT *
When You purchased Your STMICROELECTRONICS ("ST") Evaluation Board, You entered
into an Evaluation Board License Agreement ("Agreement") with ST.
You may install the SEGGER J-Link software ("J-Link") onto Your ST
Evaluation Board only if You  agree to this amendment ("Amendment")
of the Agreement.
STMICROELECTRONICS ("ST") hereby authorizes You to install
SEGGER J-Link software ("J-Link") onto Your ST Evaluation Board,
as an exception to the terms and conditions of the Agreement.
You acknowledge and agree that J-Link is provided solely by SEGGER,
and not by ST. You acknowledge and agree that ST does not endorse, recommend,
nor provide any assurance or warranty, whether express
or implied, in relation to J-Link or its use with Your ST Evaluation Board.
You acknowledge and agree that ST shall not be liable for any direct, indirect,
special, incidental or consequential damages resulting from Your use of J-Link,
even if advised of the possibility thereof.
This Amendment shall be governed, construed and enforced in accordance with the
laws of Switzerland.
Except as expressly set forth in this Amendment, the Agreement remains unchanged
and continues in full force and effect.

=============================================================================

I hereby accept the amendment to the Evaluation Board License Agreement
provided by STMICROELECTRONICS ("ST")(A)ccept / (D)ecline
Selection>A

[0] Quit
[1] Upgrade to J-Link
[2] Update J-Link firmware
[3] Restore ST-Link
Selection>1

Preparing for FW update (can take up to 10 seconds)...O.K.
Identifying ST-LINK variant...O.K.: ST-LINK/V2-1
Performing firmware update...O.K.

J-Flash Lite

Para descargar nuestro archivo ejecutable en la placa NUCLEO-G071RB vamos a emplear una herramienta de software de SEGGER J-Flash Lite. Que previamente se instaló en el artículo Visual Studio Code for ARM with CMake #1 Instalación.

Seleccionamos el MCU y la interfaz SWD seleccionamos OK.

Seleccionar MCU para flashear

Cargamos la ruta donde esta nuestro archivo ejecutable en Data File y seleccionamos Program Device.

Realiza la descarga en MCU

Ahora podemos ver nuestra placa NUCLEO-G071RB en funcionamiento con el simple Toggle del LED4.

Para un entorno de desarrollo necesitamos poder realizar la compilación el flasheo y la depuración desde un mismo entorno en este caso Visual Studio Code.

Crearemos un archivo J-Link commander con los comando necesario para realizar la descarga de nuestro archivo ejecutable de manera automática mediante JLink.exe.

J-Link commander File

Podemos ejecutar JLink.exe desde Visual Studio Code, para indicarle que acción realizar es necesario indicarle mediante comandos, así que para esto crearemos el archivo donde le indicamos que realice la programación de la placa NUCLEO-G071RB de manera automática.

Que comandos se pueden utilizar, en la documentación J-Link Commander encontramos una descripción.

Run Flash

En la carpeta .vscode de nuestro proyecto vamos a crear un archivo que llamare Run_Flash.jlink con el siguiente contenido:

si 1
speed 4000
r
h
loadbin  C:VSC_G071RB\build\VSC_G071RB.bin,0x08000000
rx 100
exit

Lo más importante en el archivo es colocar la ruta dónde se encuentra nuestro archivo ejecutable en formato binario así como la dirección inicial de flash desde donde se realizará la programación.

Para realizar la programación de nuestra placa abrimos una terminal en Visual Studio Code para ejecutar el siguiente comando indicándole nuestro hardware y la ruta del archivo J-Link Commander.

Ejecutamos el comando y esto comienza la programación en la placa, en las primeras ocasiones veremos la siguiente ventana solo hay que aceptar para que el proceso continúe.

Términos de J-Link
C:\VSC_G071RB>JLink.exe -device STM32G071RB -CommandFile C:\VSC_G071RB\.vscode\Run_Flash.jlink
SEGGER J-Link Commander V7.80b (Compiled Sep 20 2022 17:11:36)
DLL version V7.80b, compiled Sep 20 2022 17:10:13


J-Link Command File read successfully.
Processing script file...
J-Link>si 1
J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link STLink V21 compiled Aug 12 2019 10:29:20      
Hardware version: V1.00
J-Link uptime (since boot): N/A (Not supported by this model)  
S/N: 779525353
VTref=3.300V
Selecting SWD as current target interface.
J-Link>speed 4000
Selecting 4000 kHz as target interface speed
J-Link>r
Target connection not established yet but required for command.
Device "STM32G071RB" selected.


Connecting to target via SWD
InitTarget() start
InitTarget() end
Found SW-DP with ID 0x0BC11477
DPv0 detected
CoreSight SoC-400 or earlier
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x04770031)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xF0000000
CPUID register: 0x410CC601. Implementer code: 0x41 (ARM)
Found Cortex-M0 r0p1, Little endian.
FPUnit: 4 code (BP) slots and 0 literal slots
CoreSight components:
ROMTbl[0] @ F0000000
[0][0]: E00FF000 CID B105100D PID 000BB4C0 ROM Table
ROMTbl[1] @ E00FF000
[1][0]: E000E000 CID B105E00D PID 000BB008 SCS
[1][1]: E0001000 CID B105E00D PID 000BB00A DWT
[1][2]: E0002000 CID B105E00D PID 000BB00B FPB
Cortex-M0 identified.
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>h
PC = FFFFFFFE, CycleCnt = 00000000
R0 = FFFFFFFF, R1 = FFFFFFFF, R2 = FFFFFFFF, R3 = FFFFFFFF
R4 = FFFFFFFF, R5 = FFFFFFFF, R6 = FFFFFFFF, R7 = FFFFFFFF
R8 = FFFFFFFF, R9 = FFFFFFFF, R10= FFFFFFFF, R11= FFFFFFFF
R12= FFFFFFFF
SP(R13)= FFFFFFFC, MSP= FFFFFFFC, PSP= FFFFFFFC, R14(LR) = FFFFFFFF
XPSR = F1000000: APSR = NZCVq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 00000000, CONTROL = 00, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00
FPU regs: FPU not enabled / not implemented on connected CPU.
J-Link>loadbin  C:\VSC_G071RB\build\VSC_G071RB.bin  ,0x08000000
'loadbin': Performing implicit reset & halt of MCU.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
Downloading file [C:\VSC_G071RB\build\VSC_G071RB.bin]...
J-Link: Flash download: Bank 0 @ 0x08000000: 1 range affected (4096 bytes)
J-Link: Flash download: Total: 0.259s (Prepare: 0.071s, Compare: 0.071s, Erase: 0.000s, Program & Verify: 0.090s, Restore: 0.025s)
J-Link: Flash download: Program & Verify speed: 44 KB/s
O.K.
J-Link>rx 100
Reset delay: 100 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>exit

Script processing completed.

OnDisconnectTarget() start
OnDisconnectTarget() end

Run Erase Flash

De la misma forma crearemos otro archivo para realizar un borrado de toda la memoria flash en la placa, vamos a crear el archivo en la carpeta .vscode llamado Run_Erase_Flash.jlink con el siguiente contenido:

si 1
speed 4000
r
h
Erase 0x08000000, 0x0801FFFF
rx 100
exit

El comando Erase necesita conocer el mapeo de la memoria flash en el MCU. La dirección inicial y final que definen la sección donde se realizara el borrado de la memoria flash.

Esto depende del tamaño de memoria flash del hardware en cuestión en nuestro caso para la placa NUCLEO-G071RB es la siguiente:

Mapeo de memoria STM32G071RB
Contenido directorio vscode

Para realizar el borrado de la memoria ejecutamos el siguiente comando desde la terminal:

Vemos el proceso de borrado de flash en la terminal.

C:\VSC_G071RB>JLink.exe -device STM32G071RB -CommandFile C:\VSC_G071RB\.vscode\Run_Erase_Flash.jlink
SEGGER J-Link Commander V7.80b (Compiled Sep 20 2022 17:11:36)
DLL version V7.80b, compiled Sep 20 2022 17:10:13


J-Link Command File read successfully.
Processing script file...
J-Link>si 1
J-Link connection not established yet but required for command.
Connecting to J-Link via USB...O.K.
Firmware: J-Link STLink V21 compiled Aug 12 2019 10:29:20      
Hardware version: V1.00
J-Link uptime (since boot): N/A (Not supported by this model)  
S/N: 779525353
VTref=3.300V
Selecting SWD as current target interface.
J-Link>speed 4000
Selecting 4000 kHz as target interface speed
J-Link>r
Target connection not established yet but required for command.
Device "STM32G071RB" selected.


Connecting to target via SWD
InitTarget() start
InitTarget() end
Found SW-DP with ID 0x0BC11477
DPv0 detected
CoreSight SoC-400 or earlier
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x04770031)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0xF0000000
CPUID register: 0x410CC601. Implementer code: 0x41 (ARM)
Found Cortex-M0 r0p1, Little endian.
FPUnit: 4 code (BP) slots and 0 literal slots
CoreSight components:
ROMTbl[0] @ F0000000
[0][0]: E00FF000 CID B105100D PID 000BB4C0 ROM Table
ROMTbl[1] @ E00FF000
[1][0]: E000E000 CID B105E00D PID 000BB008 SCS
[1][1]: E0001000 CID B105E00D PID 000BB00A DWT
[1][2]: E0002000 CID B105E00D PID 000BB00B FPB
Cortex-M0 identified.
Reset delay: 0 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>h
PC = 08000350, CycleCnt = 00000000
R0 = FFFFFFFF, R1 = FFFFFFFF, R2 = FFFFFFFF, R3 = FFFFFFFF
R4 = FFFFFFFF, R5 = FFFFFFFF, R6 = FFFFFFFF, R7 = FFFFFFFF
R8 = FFFFFFFF, R9 = FFFFFFFF, R10= FFFFFFFF, R11= FFFFFFFF
R12= FFFFFFFF
SP(R13)= 20009000, MSP= 20009000, PSP= FFFFFFFC, R14(LR) = FFFFFFFF
XPSR = F1000000: APSR = NZCVq, EPSR = 01000000, IPSR = 000 (NoException)
CFBP = 00000000, CONTROL = 00, FAULTMASK = 00, BASEPRI = 00, PRIMASK = 00
FPU regs: FPU not enabled / not implemented on connected CPU.
J-Link>Erase 0x08000000, 0x0801FFFF
'erase': Performing implicit reset & halt of MCU.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
Erasing selected range...
J-Link: Flash download: Total time needed: 1.594s (Prepare: 0.079s, Compare: 0.000s, Erase: 1.475s, Program: 0.000s, Verify: 0.000s, Restore: 0.038s)
J-Link: Flash download: 
Flash sectors within Range [0x08000000 - 0x0801FFFF] deleted.
Erasing done.
J-Link>rx 100
Reset delay: 100 ms
Reset type NORMAL: Resets core & peripherals via SYSRESETREQ & VECTRESET bit.
Reset: Halt core after reset via DEMCR.VC_CORERESET.
Reset: Reset device via AIRCR.SYSRESETREQ.
J-Link>exit

Script processing completed.

OnDisconnectTarget() start
OnDisconnectTarget() end

Ahora estamos más cerca de tener el entorno listo, para descargar el código en la placa necesitamos ejecutar el comando que invocara un archivo J-Link Commander pero aún no es totalmente automático porque es necesario teclear el comando justo como los comandos de CMake para iniciar la compilación.

Veremos como automatizar estos comandos desde Visual Studio Code.



Card image cap
Guillermo Garcia I am an embedded systems software engineer. I like constant learning, IoT systems and sharing knowledge


Comentarios... no existen comentarios.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

Subscribe


Subscribe to receive the latest content.
Loading