Month: March 2023

Almost 13 years later a got a new Olimex LPC-2378-STK board

I decided to buy an Olimex LPC2378-STK that I found on eBay because this is the first board I ran NuttX.

Initially I tried to use Segger J-Link to detect the processor, but without luck:

J-Link>con
Device "LPC2378" selected.


Connecting to target via JTAG
TotalIRLen = 4, IRPrint = 0x01
Failed to identify target. Resetting via Reset pin and trying again.
TotalIRLen = 4, IRPrint = 0x01
TotalIRLen = 4, IRPrint = 0x01
Failed to identify target. Resetting via Reset pin and trying again.
TotalIRLen = 4, IRPrint = 0x01
Cannot connect to target.
J-Link>

I started to suspect that I bought a damage board.

Then I found a comment saying that the board needs 9V power supply:
https://www.embeddedrelated.com/showthread/lpc2000/41656-1.php

But that is strange because I was powering the board using USB cable and the PWR_SEL jumper correctly selected to position 3.

Anyway I found a 9V power-supply and used that, but again JLink failed to find it.

Then I decide to test using OpenOCD with a Versaloon-Link programmer that I have here and it worked like a charm:

$ sudo openocd -f interface/vsllink.cfg -f target/lpc2378.cfg
[sudo] password for alan:
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
init_targets
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Warning - assuming default core clock 4MHz! Flashing may fail if actual core clock is different.
Info : auto-selecting first available session transport "jtag". To override use 'transport select '.
Info : Versaloon(0x15)by Simon(compiled on Jul 8 2011)
Info : USB_TO_XXX abilities: 0x0000076E:0x010001EF:0xC0000007
Info : clock speed 500 kHz
Info : JTAG tap: lpc2378.cpu tap/device found: 0x4f1f0f0f (mfg: 0x787 (), part: 0xf1f0, ver: 0x4)
Info : Embedded ICE version 7
Error: EmbeddedICE v7 handling might be broken
Info : lpc2378.cpu: hardware has 2 breakpoint/watchpoint units
Info : starting gdb server for lpc2378.cpu on 3333
Info : Listening on port 3333 for gdb connections

So next time you face some issue with JLink, try to use OpenOCD!

Compiling and Flashing NuttX on Sony Spresense board

Before we start I want to share the issue that I found following the default NuttX compilation approach (it means I forgot to update bootloader, gnss, etc).

I did the default NuttX configure/make/flash/serial console, but got this error message when NuttX was starting:

cxd56_farapiinitialize: Mismatched version: loader(17646) != Self(20585)
cxd56_farapiinitialize: Please update loader and gnssfw firmwares!!

It was happening because I missed an important step: update the board bootloader, dnnrt, gnns, etc.

So, you can void this issues that I was facing, just doing it:

Download the binaries zip file from here:

https://developer.sony.com/file/download/download-spresense-firmware-v2-4-000

… and save it inside your nuttxspace/nuttx folder/directory.

Open the Linux terminal and enter inside nuttxspace/nuttx:

$ cd ~/nuttxspace/nuttx

Extract the file:

$ unzip spresense-binaries-v2.4.0.zip

Flash all the files:

$ ./tools/flash_writer.py -c /dev/ttyUSB0 -b 115200 dnnrt-mp.espk
Please press RESET button on target board
>>> Install files ...
install -b 115200
Install binaries/dnnrt-mp.espk
|0%-----------------------------50%------------------------------100%|
######################################################################

109808 bytes loaded.
Package validation is OK.
Saving package to "dnnrt-mp"
updater# >>> Save Configuration to FlashROM ...
set bootable M0P
M0P is not valid bootable. config not changed.
updater# sync
updater# Restarting the board ...
reboot


$ ./tools/flash_writer.py -c /dev/ttyUSB0 -b 115200 gnssfw.espk 
Please press RESET button on target board
>>> Install files ...
install -b 115200
Install binaries/gnssfw.espk
|0%-----------------------------50%------------------------------100%|
######################################################################

454512 bytes loaded.
Package validation is OK.
Saving package to "gnssfw"
updater# >>> Save Configuration to FlashROM ...
set bootable M0P
M0P is not valid bootable. config not changed.
updater# sync
updater# Restarting the board ...
reboot


$ ./tools/flash_writer.py -c /dev/ttyUSB0 -b 115200 loader.espk 
Please press RESET button on target board
>>> Install files ...
install -b 115200
Install binaries/loader.espk
|0%-----------------------------50%------------------------------100%|
######################################################################

129968 bytes loaded.
Package validation is OK.
Saving package to "loader"
updater# >>> Save Configuration to FlashROM ...
set bootable M0P
M0P is not valid bootable. config not changed.
updater# sync
updater# Restarting the board ...
reboot


$ ./tools/flash_writer.py -c /dev/ttyUSB0 -b 115200 sysutil.spk 
Please press RESET button on target board
>>> Install files ...
install -b 115200
Install binaries/sysutil.spk
|0%-----------------------------50%------------------------------100%|
######################################################################

174080 bytes loaded.
Package validation is OK.
Saving package to "sysutil"
updater# >>> Save Configuration to FlashROM ...
set bootable M0P
M0P is not valid bootable. config not changed.
updater# sync
updater# Restarting the board ...
reboot

Now you can configure and compile NuttX for Spresense:

$ ./tools/configure.sh spresense:nsh
$ make -j

It will create the file nuttx.spk then flash it this way:

$ ./tools/flash_writer.py -c /dev/ttyUSB0 -b 115200 nuttx.spk 
Please press RESET button on target board
>>> Install files ...
install -b 115200
Install nuttx.spk
|0%-----------------------------50%------------------------------100%|
######################################################################

97680 bytes loaded.
Package validation is OK.
Saving package to "nuttx"
updater# >>> Save Configuration to FlashROM ...
set bootable M0P
M0P is not valid bootable. config not changed.
updater# sync
updater# Restarting the board ...
reboot

Now just run minicom (or your preferred serial console terminal) and you will get:

NuttShell (NSH) NuttX-12.0.0
nsh> uname -a
NuttX 12.0.0 c9b917bcee Mar 24 2023 17:52:28 arm spresense
nsh>

Installing python on Ubuntu

Very easy after you installed the drivers as I explained here:
https://acassis.wordpress.com/2023/02/14/getting-started-to-cuda/

$ pip install torch

Testing it:

$ python
>>> import torch
>>> use_cuda = torch.cuda.is_available()
>>> if use_cuda:
...     print('__CUDNN VERSION:', torch.backends.cudnn.version())
...     print('__Number CUDA Devices:', torch.cuda.device_count())
...     print('__CUDA Device Name:',torch.cuda.get_device_name(0))
...     print('__CUDA Device Total Memory [GB]:',torch.cuda.get_device_properties(0).total_memory/1e9)
... 
__CUDNN VERSION: 8500
__Number CUDA Devices: 1
__CUDA Device Name: NVIDIA GeForce RTX 3050 Ti Laptop GPU
__CUDA Device Total Memory [GB]: 4.094164992
>>>

Bingo! We have CUDA support!!!

Testing AI-Thinker ESP32 Audio Kit V2.2 A247



This is the output of serial port (/dev/ttyUSB0) after pressing RST button:

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x1f (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DOUT, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6792
load:0x40078000,len:12072
load:0x40080400,len:6708
entry 0x40080778
I (75) boot: Chip Revision: 3
I (75) boot_comm: chip revision: 3, min. bootloader chip revision: 0
I (40) boot: ESP-IDF v3.3.2-107-g722043f73-dirty 2nd stage bootloader
I (40) boot: compile time 11:08:03
I (41) boot: Enabling RNG early entropy source...                               
I (46) boot: SPI Speed      : 40MHz                                             
I (51) boot: SPI Mode       : DOUT                                              
I (55) boot: SPI Flash Size : 4MB                                               
I (59) boot: Partition Table:                                                   
I (62) boot: ## Label            Usage          Type ST Offset   Length         
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000       
I (77) boot:  1 phy_init         RF data          01 01 0000f000 00001000       
I (85) boot:  2 factory          factory app      00 00 00010000 00100000       
I (92) boot: End of partition table                                             
I (96) boot_comm: chip revision: 3, min. application chip revision: 0           
I (103) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1f670 (1p
I (159) esp_image: segment 1: paddr=0x0002f698 vaddr=0x3ffb0000 size=0x00978 ( d
I (160) esp_image: segment 2: paddr=0x00030018 vaddr=0x400d0018 size=0x22788 (1p
I (217) esp_image: segment 3: paddr=0x000527a8 vaddr=0x3ffb0978 size=0x014f8 ( d
I (220) esp_image: segment 4: paddr=0x00053ca8 vaddr=0x40080000 size=0x00400 ( d
I (224) esp_image: segment 5: paddr=0x000540b0 vaddr=0x40080400 size=0x0a190 ( d
I (257) boot: Loaded app from partition at offset 0x10000                       
I (257) boot: Disabling RNG early entropy source...                             
                                                                                
################################################                                
arch:ESP32, 3                                                                   
compile_time:Ai-Thinker|B&T                                                     
ble_mac:b8D61A59XXXX                                                            
wifi_mac:b8D61A59XXXX                                                           
sdk_version:v3.3.2-107-g722043f73-dirty                                         
firmware_version: v1.1.0                                                        
compile_time:Jun  8 2021 11:08:16                                               
################################################

How to copy (dump) flash memory of the ESP32

I use this command to dump the entire flash memory of my ESP32-Devkitc:

$ esptool.py --chip esp32 --port "/dev/ttyUSB0" --baud 921600 read_flash 0 0x400000 esp32_devkit_dump.bin
Serial port /dev/ttyUSB0
Connecting....
Chip is ESP32-D0WD-V3 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: b8:d6:1a:59:ab:60
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
4194304 (100 %)
4194304 (100 %)
Read 4194304 bytes at 0x0 in 51.9 seconds (646.5 kbit/s)...
Hard resetting via RTS pin...

Using OpenOCD to flash Atmega1284P

After facing issues with avrdude using the native programmer from Mega1284P-Xplained board I decided to test OpenOCD to flash the board.

Using a J-Link V8 programmer connected to the board this way:

JLINK MEGA1284-XPLAINED

VTref VCC (5V)

TDI TDI

TMS TMS

TCK TCK

TDO TDO

RESET RESET

GND GND

$ sudo openocd -f interface/jlink.cfg -f target/atmega128.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.hex 0x00000000"
Open On-Chip Debugger 0.11.0
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "jtag". To override use 'transport select <transport>'.
Info : J-Link ARM V8 compiled Nov 28 2014 13:44:46
Info : Hardware version: 8.00
Info : VTarget = 3.319 V
Info : clock speed 4500 kHz
Info : JTAG tap: avr.cpu tap/device found: 0x1970503f (mfg: 0x01f (Atmel), part: 0x9705, ver: 0x1)
Warn : JTAG tap: avr.cpu       UNEXPECTED: 0x1970503f (mfg: 0x01f (Atmel), part: 0x9705, ver: 0x1)
Error: JTAG tap: avr.cpu  expected 1 of 1: 0x8970203f (mfg: 0x01f (Atmel), part: 0x9702, ver: 0x8)
Error: Trying to use configured scan chain anyway...
Warn : Bypassing JTAG setup events due to errors
Info : JTAG tap: avr.cpu tap/device found: 0x1970503f (mfg: 0x01f (Atmel), part: 0x9705, ver: 0x1)
Warn : JTAG tap: avr.cpu       UNEXPECTED: 0x1970503f (mfg: 0x01f (Atmel), part: 0x9705, ver: 0x1)
Error: JTAG tap: avr.cpu  expected 1 of 1: 0x8970203f (mfg: 0x01f (Atmel), part: 0x9702, ver: 0x8)
Error: Trying to use configured scan chain anyway...
Warn : Bypassing JTAG setup events due to errors
Info : device id = 0x1970503f
Info : target device is atmega1284p
auto erase enabled
wrote 55296 bytes from file nuttx.hex in 1.429967s (37.763 KiB/s)

Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections

Compiling NuttX to Microchip/Atmel MEGA1284P-XPLAINED board

I just ported NuttX to Atmel MEGA1284P-XPLAINED board using as base the moteino-mega board that is very similar. Main difference is the 16MHz crystal that became 11.0592MHz in the MEGA1284P-XPLAINED.

These are the steps to compile NuttX:

1) Install the AVR GCC toolchain:

$ sudo apt install gcc-avr avr-libc

2) Configure NuttX to compile to this board:

$ ./tools/configure.sh mega1284p-xplained:nsh
$ make

3) Flash the generated nuttx.hex file:

$ sudo avrdude -p atmega1284p -c avr910 -P /dev/ttyACM0 -b57600 -F -u -U flash:w:nuttx.hex:i

For some strange reason it is failing:

avrdude: avr910_recv(): programmer is not responding
Found programmer: Id = "AVRBOOT"; type = S
    Software Version = 1.5; Hardware Version = ?.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize = 256 bytes.

Programmer supports the following devices:

avrdude: warning: selected device is not supported by programmer: m1284p
avrdude: error: programmer did not respond to command: select device
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.09s

avrdude: Device signature = 0x97050d
avrdude: Expected signature for ATmega1284P is 1E 97 05
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: error: programmer did not respond to command: chip erase
avrdude: reading input file "nuttx.hex"
avrdude: writing flash (55260 bytes):

Writing | ################################################## | 100% 23.64s

avrdude: 55260 bytes of flash written
avrdude: verifying flash memory against nuttx.hex:
avrdude: load data flash data from input file nuttx.hex:
avrdude: input file nuttx.hex contains 55260 bytes
avrdude: reading on-chip flash data:

Reading
...
Reading | ################################################## | 100% 10.89s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x0d != 0x0c
avrdude: verification error; content mismatch
avrdude: error: programmer did not respond to command: leave prog mode

avrdude done.  Thank you.

So, it is not working yet, but we are near!!! Stay tuned!

Update: need to test avr-aosp project https://github.com/cbalint13/avr-aosp/

Programming the (Microchip) Atmel MEGA-1284P-XPLAINED board on Linux

First check if your board is detected (after connecting the miniusb cable in the board and your computer)

$ dmesg
...
[23916.604107] usb 3-3: new full-speed USB device number 11 using xhci_hcd
[23916.753688] usb 3-3: New USB device found, idVendor=03eb, idProduct=2122, bcdDevice=10.00
[23916.753691] usb 3-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[23916.753692] usb 3-3: Product: XPLAINED CDC
[23916.753693] usb 3-3: Manufacturer: ATMEL
[23916.753695] usb 3-3: SerialNumber: 124XXX000XXX
[23922.052622] cdc_acm 3-3:1.0: ttyACM0: USB ACM device
[23922.052654] usbcore: registered new interface driver cdc_acm
[23922.052655] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

So it created the /dev/ttyACM0 device, now we can use it to flash a new firmware or to dump the content of flash memory.

Atmel created an avrosp programmer to use with this board and according with their documentation it uses the avr911 protocol. But unfortunately the avrosp is not maintained anymore and we need to find an alternative.

I tried to use avrdude, since it supports the avr911 protocol, but for some strange reason it doesn’t work with this board. First you need to press and hold the SW0 button in the board, press and release the RESET button and after 1 second release the SW0 button. Now you can try:

$ avrdude -p atmega1284p -c avr911 -P /dev/ttyACM0 -b57600 -U flash:r:dump.hex:i

Connecting to programmer: .
Found programmer: Id = "AVRBOOT"; type = S
    Software Version = 1.5; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=256 bytes.

Programmer supports the following devices:

avrdude: error: programmer did not respond to command: select device
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you. 

After many tests I discovered that avr910 protocol works, but it prints a lot of error messages:

$ sudo avrdude -p atmega1284p -c avr910 -P /dev/ttyACM0 -b57600 -F -u -U flash:r:dump.hex:i

avrdude: avr910_recv(): programmer is not responding
Found programmer: Id = "AVRBOOT"; type = S
    Software Version = 1.5; Hardware Version = ?.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize = 256 bytes.

Programmer supports the following devices:

avrdude: warning: selected device is not supported by programmer: m1284p
avrdude: error: programmer did not respond to command: select device
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.09s

avrdude: Device signature = 0x97050d
avrdude: Expected signature for ATmega1284P is 1E 97 05
avrdude: reading flash memory:

Reading |                                                    | 0% 0.00savrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
Reading | #                                                  | 1% 0.35savrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
Reading | #                                                  | 2% 0.60savrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
Reading | ##                                                 | 3% 0.85savrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
Reading | ##                                                 | 4% 1.10savrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
avrdude: error: programmer did not respond to command: set addr
Reading | ###                                                | 5% 1.35savrdude: error: programmer did not respond to command: set addr

Note that the progress bar of Reading is advancing! Then if you wait it will finish and read the flash correctly.