dragino / LGT-92_-LoRa_GPS_Tracker

Software Source code for LoRa_GPS_Tracker
51 stars 42 forks source link

[REQUEST] Improve "Upload Firmware" instructions in UserManual PDF #10

Open didebuli opened 4 years ago

didebuli commented 4 years ago

Currently available User Manual at URL https://www.dragino.com/downloads/downloads/LGT_92/LGT-92_LoRa_GPS_Tracker_UserManual_v1.5.5.pdf does provide Firmware update instructions for Windows users only.

I'm a Linux user and I did have issues flashing LGT92 firmware from a Windows 7 VirtualBox machine. After holding the RST button and clicking on Settings button the LEDs on ST-LINK v2 adapter (cheap/clone from aliexpress) the LEDs did not stay blinking after releasing RST and I was getting different "can not communicate to device" type of errors..

Initially I thought I'm doing something wrong but later I found that some others did also have similar issues when trying to pass st-link adapter via USB pass-through to VirtualBox Windows VM. So I did have two options: search for a Windows PC I could use for flashing firmware or.. try to do it from Linux directly. I tried second option and it was actually very easy to flash it from Linux (Ubuntu 18.04)

I could not find any Dragino or LGT92 specific instructions but since I did already successfully flash other stm32 boards from Platform IO I tried adjusting the openocd command for STM32L072 and it worked.

My first attempt was to use openocd which came with Platform IO:

user@localhost:~/Documents/PlatformIO$ /home/user/.platformio/packages/tool-openocd/bin/openocd -f /home/user/.platformio/packages/tool-openocd/scripts/interface/stlink-v2.cfg -c "transport select hla_swd" -f /home/user/.platformio/packages/tool-openocd/scripts/target/stm32l0.cfg -c init -c 'program /home/user/Downloads/lgt92-1.5.3/EU868.hex verify reset exit'

xPack OpenOCD, 64-bit Open On-Chip Debugger 0.10.0+dev (2019-07-17-11:25)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
hla_swd
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 300 kHz
Info : STLINK V2J17S4 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.244710
Info : stm32l0.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x080000d4 msp: 0x20002280
STM32L0: Enabling HSI16
** Programming Started **
Info : Device: STM32L0xx (Cat.5)
Info : STM32L flash has dual banks. Bank (0) size is 96kb, base address is 0x8000000
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked

After this via serial connection in AT+CFG response LGT92 did show: "AT+VER=v1.5.3 EU868" so firmware upload was successful.

I thought that it should be even easier if one would use openocd which comes with Ubuntu 18.04 so I also tried:

$ sudo apt install openocd

And after it was installed invocing openocd with similar parameters as earlier did also work:

user@localhost:~$ openocd -f /usr/share/openocd/scripts/interface/stlink-v2.cfg -c "transport select hla_swd" -f /usr/share/openocd/scripts/target/stm32l0.cfg -c init -c 'program /home/user/Downloads/lgt92-1.5.3/EU868.hex verify reset exit'
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
hla_swd
adapter speed: 300 kHz
adapter_nsrst_delay: 100
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : clock speed 240 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.247904
Info : stm32l0.cpu: hardware has 4 breakpoints, 2 watchpoints
adapter speed: 300 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x080000d4 msp: 0x20002280
STM32L0: Enabling HSI16
adapter speed: 2500 kHz
** Programming Started **
auto erase enabled
Info : Device: STM32L0xx (Cat.5)
Info : STM32L flash has dual banks. Bank (0) size is 128kb, base address is 0x8000000
Warn : couldn't use loader, falling back to page memory writes
wrote 86016 bytes from file /home/user/Downloads/lgt92-1.5.3/EU868.hex in 11.927619s (7.042 KiB/s)
** Programming Finished **
** Verify Started **
Error: JTAG failure
Error: Error setting register
target halted due to breakpoint, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x2000002e msp: 0x20002280
verified 84356 bytes in 0.451015s (182.652 KiB/s)
** Verified OK **
** Resetting Target **
adapter speed: 300 kHz
shutdown command invoked
user@localhost:~$ 

it looks like Platform IO brings a slightly newer version which correctly detects flash bank size to be "Bank (0) size is 96kb" while Ubuntu included openocd "Bank (0) size is 128kb"

After both flashing LGT92 did work so assume that both openocd versions should be fine to use.

I would like to suggest Dragino team to add this steps to User Manual maybe as a additional section "Upload Firmware ( Linux - using Open On-Chip Debugger )"

Step1: Run from command line $ sudo apt install openocd

Step2: Download the LGT-92 firmware Image file.

Step3: Prepare openocd command , use correct path to the downloaded hex file . openocd -f interface/stlink-v2.cfg -c "transport select hla_swd" -f target/stm32l0.cfg -c init -c 'program /home/user/Downloads/lgt92-1.5.3/EU868.hex verify reset exit'

Step4: If not already done, connect LGT92 to ST-Link (instructions already available for Windows)

Step5: Press RST button and run the prepared openocd command. As soon as ST-LINK LED starts to flash release the Reset button and monitor the command result .

Successful flash attempt should typically have in output " Programming Finished and also Verified OK "

Step6: connect LGT92 via Serial and access it using serial terminal for example by running command screen /dev/ttyUSB0 9600 (apt install screen if not installed and device name/number might be different in your case)

Step7: check firmware version using AT+CFG and also you should run AT+FDR after a firmware upgrade.

Hope someone from Dragino team can easily verify this steps on Ubuntu 18.04 and ask UserManual author to add this instruction to the PDF.

(I know that not everybody uses Ubuntu, but it's typically not a problem for Linux users to adapt such steps to work on other distributions as well)

Thanks in Advance!

pukkita commented 4 years ago

Thanks a lot for taking the time to write this, @didebuli!

It helped me a lot in order to get this working under OS X, as I experienced the same problem while trying to flash from a VirtualBox Windows10 VM (Firmware seemed to be flashed but it wasn't).

You got me inspired so wanted to share the procedures to natively work under OS X (Serial comms) and on how to upgrade the firmware for those using macs:

Upgrading Dragino LGT-92 firmware under OS X

1.- Getting Set / Requirements

1.1.- Xcode

To check CLI Tools are already successfully installed:

Kratos:~ root# xcode-select
xcode-select: error: no command option given
Usage: xcode-select [options]

Print or change the path to the active developer directory. This directory
controls which tools are used for the Xcode command line tools (for example, 
xcodebuild) as well as the BSD development commands (such as cc and make).

Options:
  -h, --help                  print this help message and exit
  -p, --print-path            print the path of the active developer directory
  -s <path>, --switch <path>  set the path for the active developer directory
  --install                   open a dialog for installation of the command line developer tools
  -v, --version               print the xcode-select version
  -r, --reset                 reset to the default command line tools path

1.2.- Install your USB to TTL Adapter VCP Driver

After installation of the driver and rebooting, in order to determine which device file OS X uses to communicate with the USB Adapter:

# ls -l /dev/cu.*
crw-rw-rw-  1 root  wheel   21,   3 Feb  7 09:27 /dev/cu.Bluetooth-Incoming-Port
crw-rw-rw-  1 root  wheel   21,   5 Feb  7 09:27 /dev/cu.LG-PK585-SPPDev1-3
crw-rw-rw-  1 root  wheel   21,   7 Feb  7 09:27 /dev/cu.LG-PK585-SPPDev2-2
crw-rw-rw-  1 root  wheel   21,  33 Feb 13 12:53 /dev/cu.SLAB_USBtoUART

We see here our device is /dev/cu.SLAB_USBtoUART

Tip: If you use OS X >= 10.13 and don't see any device file, check https://developer.apple.com/library/archive/technotes/tn2459/_index.html


1.3.- Serial Comm Utility and Testing

OS X has serial comm CLI utilities installed by default, like cu and screen that we can use for serial communication to finish the upgrade procedure.

To test serial communication is working using cu, open Applications/Utilities/Terminal and issue:

Kratos $ cu -s 9600 -l /dev/cu.SLAB_USBtoUART -h
Connected.
atz^M
Roll=1.46  Pitch=3.32
North: 37.408978
West: -6.164614

[265654490]***** UpLinkCounter= 1031 *****
[265654890]TX on freq 867100000 Hz at DR 5
Update Interval: 30000 ms
[265654957]txDone

Wait a little, you should see LGT-92 activity debug scrolling; if so, USB 2 TTL communication is working!

Tips:

Captura de pantalla 2020-02-13 a las 14 28 05


2.- Homebrew / openocd tools Installation

2.1.- Homebrew

Homebrew is a Opensource package manager / Opensource repository, which works similar to apt, yum, rpm, etc, bringing easy to install/build packages for most OpenSource Projects under OS X.

We will use it to easily install openocd tools in OS X, which will allow us to flash the LGT-92.

If XCode is already installed, usually all that is required to install homebrew is to:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.2.- Install openocd tools using homebrew

Kratos $ brew install openocd

A bunch of text will scroll in the screen (too much to paste it here), check that at the end:

...
==> Pouring open-ocd-0.10.0.high_sierra.bottle.2.tar.gz
🍺  /usr/local/Cellar/open-ocd/0.10.0: 632 files, 4.6MB

3.- Flash Firmware Procedure

Comprises:

3.1- Download Firmware

Note the path where you save the file, /Users/myusername/Documents/_LORA/DRAGINO/EU868.hex in this example; you'll need to substitute this in the openocdcommand on step 2 of Flash Firmware section.

Tips:

3.2.- Flash Firmware

Captura de pantalla 2020-02-13 a las 14 29 16

Wiring:

ST-LINK v2 5.0v  <--> Dupont red pin
ST-LINK v2 GND   <--> Dupont black pin
ST-LINK v2 SWCLK <--> Dupont green pin
ST-LINK v2 SWDIO <--> Dupont white pin

Example procedure to craft the openocd command line: Dragino

Kratos:~ root# openocd -f interface/stlink-v2.cfg -c "transport select hla_swd" -f target/stm32l0.cfg -c init -c 'program /Users/myusername/Documents/_LORA/DRAGINO/EU868.hex verify reset exit'
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
hla_swd
adapter speed: 300 kHz
adapter_nsrst_delay: 100
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
none separate
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : clock speed 240 kHz
Info : STLINK v2 JTAG v31 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.224258
Info : stm32l0.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : Unable to match requested speed 300 kHz, using 240 kHz
adapter speed: 240 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x080000d4 msp: 0x20002260
STM32L0: Enabling HSI16
Info : Unable to match requested speed 2500 kHz, using 1800 kHz
Info : Unable to match requested speed 2500 kHz, using 1800 kHz
adapter speed: 1800 kHz
** Programming Started **
auto erase enabled
Info : Device: STM32L0xx (Cat.5)
Info : STM32L flash has dual banks. Bank (0) size is 128kb, base address is 0x8000000
Warn : couldn't use loader, falling back to page memory writes
wrote 86016 bytes from file /Users/pacopepe/Documents/_LORA/DRAGINO/EU868.hex in 12.344066s (6.805 KiB/s)
** Programming Finished **
** Verify Started **
Error: JTAG failure
Error: Error setting register
target halted due to breakpoint, current mode: Handler HardFault
xPSR: 0x61000003 pc: 0x2000002e msp: 0x20002260
verified 84356 bytes in 0.451254s (182.556 KiB/s)
** Verified OK **
** Resetting Target **
Info : Unable to match requested speed 300 kHz, using 240 kHz
Info : Unable to match requested speed 300 kHz, using 240 kHz
adapter speed: 240 kHz
in procedure 'program' 
in procedure 'reset' called at file "embedded:startup.tcl", line 529
in procedure 'ocd_bouncer'

3.3.- Reset to Factory Defaults

To finish the firmware upgrade:

AT+FDR^M
LGT-92 Device
Image Version: v1.5.3
LoRaWan Stack: DR-LWS-002
Frequency Band: EU868
DevEui= XX XX XX XX XX XX XX XX
Please set the parameters or reset Device to apply change
LGT-92 Device
Image Version: v1.5.3
LoRaWan Stack: DR-LWS-002
Frequency Band: EU868
DevEui= XX XX XX XX XX XX XX XX

[2085]***** UpLinkCounter= 0 *****
[2485]TX on freq 868500000 Hz at DR 5
[2551]txDone
[7541]RX on freq 868500000 Hz at DR 5
[7625]rxDone
Rssi= -58
JOINED

Join Accept:
DevAddr:23 08 23 e0
Rx1DrOffset:0
Rx2Datarate:3
ReceiveDelay1:1000 ms
ReceiveDelay2:2000 ms

[19484]Roll=0.73  Pitch=-4.72
[19535]North: 37.408657
[19635]West: -6.164669
[19736]PDOP is 1.31

Yay! LGT-92 Successfully flashed using OS X to v1.5.3.

@dragino Feel free to include this in the manual.

FoxWhiskey commented 1 year ago

Thank you all for your ideas!

As a matter of fact I was wondering, too, why there is no official document on flashing STM32 devices under Linux. Not being aware of this Github issue I found a third way to flash STM32 devices (an LGT-92 to be more precise), which I like to share with interested readers...

Using stlink-org/stlink

The project consists of a collection of command line tools to flash STM32* devices. Either download the sources from Github and build the binaries yourself or make use of your system's standard repositories to install precompiled binaries. I've done the latter, not experiencing any issues.
So, on Debian (& flavoured)

sudo apt-get install stlink-tools

Connect LGT92 to your System

Setup your Flash-environment with a flash-adaptor and jumper cables in the same way as noted in the examples above

Flashing procedure

  1. Prepare a probe command in a terminal window without (!) pressing Enter:
    stinfo --probe
  2. Press and hold the RESET-button on your LGT92. Then press ENTER and watch adaptor-LED
  3. As soon as LED starts blinking, release RESET-button. This is important!
  4. Check output on screen. If the output suggests that the device has not been recognised, enter stinfo --probe again, without pressing RESET-button.
    Check, whether chipId == 0x447
  5. When the device has been detected, flash new firmware using following command
    st-flash --reset --format ihex write [file.hex]
    while file.hex is the human readable firmware update file from DRAGINO's website.

Terminal output on successful flash

user@TheDude:~/LGT92$ st-flash --reset --format ihex write EU868.hex 
st-flash 1.6.0
2022-11-20T21:56:35 INFO common.c: Loading device parameters....
2022-11-20T21:56:35 INFO common.c: Device connected is: L0x Category 5 device, id 0x20086447
2022-11-20T21:56:35 INFO common.c: SRAM size: 0x5000 bytes (20 KiB), Flash: 0x30000 bytes (192 KiB) in pages of 128 bytes
2022-11-20T21:56:35 INFO common.c: Attempting to write 93456 (0x16d10) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08016d00 erased
2022-11-20T21:56:39 INFO common.c: Finished erasing 731 pages of 128 (0x80) bytes
2022-11-20T21:56:39 INFO common.c: Starting Half page flash write for STM32L core id
2022-11-20T21:56:39 INFO flash_loader.c: Successfully loaded flash loader in sram
1460/1460 halfpages written
2022-11-20T21:56:50 INFO common.c: Starting verification of write complete
2022-11-20T21:56:52 INFO common.c: Flash written and verified! jolly good!
user@TheDude:~/LGT92$

stlink-gui

A GUI is available for the stlink-toolset as well. Unfortunately, the docs are not very detailed and I found issues which eventually detered me from using it (not to risk bricking my device):

Happy flashing!

-bernie