Sunday, June 16, 2019

An Ada Client & Server on the STM32WB55


Ada WB55 Client & Server

Following on from the last blog posting, I now have a client and server implementation in Ada. Mine is not quite as fancy as ST's, ST's allows role reversal where the client can run on the larger board (MB1355C) and the server on the USB dongle (MB1293C). I only support server on MB1355C and client on MB1293C.

Getting the code & building

You will need gnat2018 or gnat2019 from AdaCore:

https://www.adacore.com/download

Once that's installed, you will need some library code and the STM32 dir I use:

git clone https://github.com/morbos/Ada_Drivers_Library.git
git clone https://github.com/morbos/embedded-runtimes.git
git clone https://github.com/morbos/STM32.git
mv ../embedded-runtimes Ada_Drivers_Library
cd STM32/WB/WB55/cli_serv_wb55
make

Once make finishes you get 2 ELF32 files in the obj/Debug dir.

admin@ubuntu_1604:/tmp/STM32/WB/WB55/cli_serv_wb55$ make
rm -f obj/Debug/client_wb55x
(export LOADER=ROM_WB55x; gprbuild client_wb55x.gpr)
Link
   [link]         client_wb55x.adb
Memory region         Used Size  Region Size  %age Used
           flash:       93008 B         1 MB      8.87%
           sram1:      102792 B       192 KB     52.28%
          sram2a:          0 GB        32 KB      0.00%
          sram2b:          0 GB        32 KB      0.00%
(cd obj/Debug; arm-eabi-objdump -d client_wb55x >client_wb55x.lst; arm-eabi-objdump -s client_wb55x >client_wb55x.dmp; arm-eabi-gcc-nm -an client_wb55x >client_wb55x.nm; arm-eabi-objcopy -Obinary client_wb55x client_wb55x.bin)
rm -f obj/Debug/server_wb55x
(export LOADER=ROM_WB55x; gprbuild server_wb55x.gpr)
Link
   [link]         server_wb55x.adb
Memory region         Used Size  Region Size  %age Used
           flash:       93752 B         1 MB      8.94%
           sram1:      108056 B       192 KB     54.96%
          sram2a:          0 GB        32 KB      0.00%
          sram2b:          0 GB        32 KB      0.00%
(cd obj/Debug; arm-eabi-objdump -d server_wb55x >server_wb55x.lst; arm-eabi-objdump -s server_wb55x >server_wb55x.dmp; arm-eabi-gcc-nm -an server_wb55x >server_wb55x.nm; arm-eabi-objcopy -Obinary server_wb55x server_wb55x.bin)

Each of these ELF32 files can be flashed on the board.

Openocd

To flash the MB1355C use the ST-Link USB connector (the silkscreen is on the bottom of the board).

You will need openocd-0.10.0 that is on my github.

Modify the st_nucleo_wb.tcl in the tcl/board dir. You want to make sure the v2-1 line is uncommented. 

From:
# source [find interface/stlink-v2-1.cfg]
source [find interface/stlink-v2.cfg]

To:

source [find interface/stlink-v2-1.cfg]
#source [find interface/stlink-v2.cfg]

Then you can attach:

root@pi3:~/openocd-0.10.0/tcl# ../src/openocd -f board/st_nucleo_wb55.cfg Open On-Chip Debugger 0.10.0 Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD adapter speed: 2000 kHz adapter_nsrst_delay: 100 none separate none separate Info : Unable to match requested speed 2000 kHz, using 1800 kHz Info : Unable to match requested speed 2000 kHz, using 1800 kHz Info : clock speed 1800 kHz Info : STLINK v2 JTAG v32 API v2 SWIM v22 VID 0x0483 PID 0x374B Info : using stlink api v2 Info : Target voltage: 3.268721 Info : stm32wb.cpu: hardware has 6 breakpoints, 4 watchpoints

Flashing & Debug

admin@ubuntu_1604:/.share/CACHEDEV1_DATA/Ada/STM32/WB/WB55/cli_serv_wb55$ arm-eabi-gdb obj/Debug/server_wb55x
GNU gdb (GDB) 8.3 for GNAT Community 2019 [rev=gdb-8.3-ref-194-g3fc1095]
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
See your support agreement for details of warranty and support.
If you do not have a current support agreement, then there is absolutely
no warranty for this version of GDB.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=arm-eabi".
Type "show configuration" for configuration details.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from obj/Debug/server_wb55x...
(gdb) target extended-remote 10.0.1.241:3333
Remote debugging using 10.0.1.241:3333
0x0800e02e in system.bb.board_support.interrupts.power_down ()
    at /.share/CACHEDEV1_DATA/Ada/STM32/WB/WB55/cli_serv_wb55/Ada_Drivers_Library/embedded-runtimes/base_runtimes/ravenscar-full/gnarl-common/s-bbbosu.adb:402
402          Asm ("wfi", Volatile => True);
(gdb) monitor reset halt
Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
adapter speed: 1800 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08010968 msp: 0x2001a620
(gdb) load
Loading section .text, size 0x121d0 lma 0x8000000
Loading section .ARM.extab, size 0xe34 lma 0x80121d0
Loading section .ARM.exidx, size 0xe98 lma 0x8013004
Loading section .rodata, size 0x31e8 lma 0x8013ea0
Loading section .data, size 0x78c lma 0x8017088
Start address 0x8010968, load size 96272
Transfer rate: 22 KB/sec, 10696 bytes/write.
(gdb) monitor reset init
Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
adapter speed: 1800 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08010968 msp: 0x2001a620
Unable to match requested speed 2000 kHz, using 1800 kHz
Unable to match requested speed 2000 kHz, using 1800 kHz
adapter speed: 1800 kHz
(gdb) b main
Breakpoint 1 at 0x800060a: file b__server_wb55x.adb, line 276.
(gdb) b __gnat_last_chance_handler
Breakpoint 2 at 0x800065a: file /.share/CACHEDEV1_DATA/Ada/STM32/WB/lch_sfp/led/last_chance_handler.adb, line 48.
(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.

Breakpoint 1, main () at b__server_wb55x.adb:276
276       Ensure_Reference : aliased System.Address := Ada_Main_Program_Name'Address;
(gdb) 

To flash the MB1293C attach using the other text line of st_nucleo_wb.tcl and use a similar flashing process except flash the client_wb55


Thursday, June 13, 2019

Ada on the STM32WB

STM32WB

This is a new part family that merges an L series microcontroller with the BlueNRG-MS controller realized now as a single die. The microcontroller can run at 64Mhz has 1MB of flash and 256KB of ram. The wireless portion (I say wireless since its not limited to BT/BLE but also supports Thread and Zigbee), runs on a Cortex-M0+ controller and shares the top of the 1MB of flash for its (encrypted) FW. The M0's FW size seems about 256KB or so. The CM4F and CM0+ communicate by using some of the 256KB of ram as a mailbox. This is supported by HW (IPCC) that signals interrupts from one side to the other when data is ready. In the past, as on the SensorTile, the BlueNRG-MS was connected by SPI and that was the transport, now its done by mailbox and hw signalling.

Ada

Can we do it again? Can we get Ada running usefully on an WB series part with the added burden of the new control over BT/BLE? I will save the readers time here to say, yes, its possible! further, its working. Here is some interesting info, Ada's package system cleanly separates modules from one another so, I was able to smoothly migrate the BT work over from the SensorTile to the WB just as I had envisioned. The trick here was to recognize that ST was not going to re-invent the wheel here, they would use 99% of the working BlueNRG-MS stack over on the WB. That means, opcodes are the same, events are the same. All the data structures that had been done for those were a drop in. This saves me months of weekend dev time. Of course, there are some differences, these are relatively minor wrt the BT messaging.

SVD

I carp about SVD files a bunch. To me I think they are the key to getting Ada going on a a new target. ST has been good over the years at generating SVD files. Why should the WB be any different? Well, it is different. There is no SVD file on their website as of this writing. (odd since they released one for the complex STM32MP157 with hundreds of blocks). I need an SVD file so what are we going to do to get Ada going on the WB55? Well, the reference manual defines all the regs... maybe... well that's what I did I cut and paste into block txt files all the reg defns, bugs and all (yes there are loads of datasheet bugs). Once all the hw blocks were assembled I then hacked a Ruby script to convert them into xml fragments and assembled the whole shebang as the SVD file. Its part of the dir trees called out below. svd2ada is happy with the file and I have been using it smoothly for bringup and for my usual technique of using SVD files to parse raw GDB dumps back to ascii dotted format for easy diffing (see my blog entry on that: http://www.hrrzi.com/2017/07/arm-cortex-svd-files-lot-of-goodness.html  ).

An update on the SVD topic, Pierre Le Corre of ST pointed out the the STM32CubeIDE has as subdir where the SVD file can be found. Indeed its there along with all of the F and L series parts.

STM32CubeIDE_1.0.1/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.productdb.debug_1.0.0.201904021149/resources/cmsis/STMicroelectronics_CMSIS_SVD

Hardware & Demo FW

ST sells a nucleo board eval kit for the WB55. It has two boards in it. A nucleo board and a USB dongle. Out of the box they have a client server demo. The bigger board is the server and the dongle is a client. The dongle on a button press scans and connects to the server. Once connected, the button (SW1) toggles the blue LED on the other board. So, each can flip the led on the other one. SW2 on the bigger board changes the rate the radio refreshes. In this mode, the LED takes a little longer to toggle.

Ada client

I crafted a workalike of the ST client that runs on the dongle. Here is the larger nucleo running STs server code communing with my Ada client on the dongle.



The Ada client performs all the functions as stated in the readme ST provides:

 - The Peripheral device (BLE_p2pServer) starts advertising (during 1 minute), the green led blinks for each advertising event.
 - The Central device (BLE_p2pClient) starts scanning when pressing the User button (SW1) on the USB Dongle board. 
   - BLE_p2pClient blue led becomes on. 
   - Scan req takes about 5 seconds. *
   - Make sure BLE_p2pServer advertises, if not press reset button or switch off/on to restart advertising.
 - Then, it automatically connects to the BLE_p2pServer. 
   - Blue led turns off and green led starts blinking as on the MB1355C. Connection is done.
 - When pressing SW1 on a board, the blue led toggles on the other one.
   - The SW1 button can be pressed independently on the GATT Client or on the GATT Server.
 - When the server is located on a MB1355C, the connection interval can be modified from 50ms to 1s and vice-versa using SW2. 
 - The green led on the 2 boards blinks for each advertising event, it means quickly when 50ms and slowly when 1s. 
 - Passing from 50ms to 1s is instantaneous, but from 1s to 50ms takes around 10 seconds.
 - The SW1 event, switch on/off blue led, depends on the connection Interval event. 
   - So the delay from SW1 action and blue led change is more or less fast.

* I should say the Ada client scans faster since it abandons the scan when it finds the server.

Code

Order a WB55 nucleo board and get started with Ada running a BLE stack!

## Building on Linux gnat2018 or gnat2019 needs to be installed first
git clone https://github.com/morbos/Ada_Drivers_Library.git
git clone https://github.com/morbos/embedded-runtimes.git
git clone https://github.com/morbos/STM32.git
mv ../embedded-runtimes Ada_Drivers_Library
cd STM32/WB/WB55/client_wb55
make

Flashing & Debugging

To flash the code to the USB dongle, openocd needs to be used. First there is a hookup:


Four wires from the ST Link V2.0 over to the USB dongle. If you zoom in a bit you can id the wires that need to go where.

Next you need my version of openocd:


I built it on a RaspberryPi3 as so:

./configure --enable-ftdi --enable-stlink --enable-ti-icdi --enable-jlink

Then make

Add other --enable-xyz's if you have other targets not called out above.

Finally run it. I usually cd to the tcl dir:

../src/openocd -f board/st_nucleo_wb55.cfg
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 2000 kHz
adapter_nsrst_delay: 100
none separate
none separate
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : Unable to match requested speed 2000 kHz, using 1800 kHz
Info : clock speed 1800 kHz
Info : STLINK v2 JTAG v17 API v2 SWIM v4 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.248645
Info : stm32wb.cpu: hardware has 6 breakpoints, 4 watchpoints

Future work

This is a work in progress.. I plan to swap out the server on the larger nucleo board with an Ada version. Stay tuned.