STM32F051K8-SWDCOM

### Project: STM32F051K8-SWDCOM #### File Name: STM32F051K8-SWDCOM.md #### Started: Sat 12 Sep 2020 16:46:55 AEST

<pre> Copyright 2019 Terry Porter terry@tjporter.com.au

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1) Redistributions of source code must retain the this copyright notice, this list of conditions and the following disclaimer. 2) Redistributions in binary form must reproduce this copyright notice, this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS’’ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. </pre>

#### This project: A STM32 technology demonstrator showcasing the advantages of using the SWD (Serial Wire Debugging) interface as a Forth Terminal instead of the legacy serial USART. This is made to use a STMicro STM32F0 Discovery board but can use a SWD-USB Programmer and a STM32F0xx mcu instead.

#### Credits: - SWDcom Demo written by Terry Porter <terry@tjporter.com.au> - SWDcom by Jan Bramkamp <https://github.com/Crest/swdcom> - Mecrisp-Stellaris by Matthias Koch https://sourceforge.net/projects/mecrisp/

#### Serial communications. Forth is one of the oldest programming languages in existence having been developed in the early 70’s. In those days a computer running Forth usually communicated with a serial device, perhaps a Teletype or later a Wyse dedicated electronic terminal to allow interactive development and program uploading.

The availability of the IBM PC in the early 80’s (and other readily available portable computers) allowed the use of terminal emulators such as Picocom, Minicom etc running on the PC itself instead of dedicated terminals.

#### Developer unfriendly Usart based Forth legacy serial communications issues. - Slow, most Forths don’t go above 115200 Baud. - No serial flow control. This requires ‘end of line delays’ to prevent the Forth compiler ‘choking’ on uploaded source code. These can be as large as 200 milliseconds (depending on the speed of the Forth mcu) making source uploads glacial. - Changing the mcu clock speed is tedious as the usart Baudrate is derived from it. - An external ‘USB to 3.3v Serial Dongle’ is required for most ‘development boards’ and stand alone mcus to talk to the PC. - Resetting the mcu requires entering ‘reset’ into the Forth console

#### Making Forth developer friendly in 2020. Swdcom, provides a SWD based usart replacement along with a PC based terminal client that works with Linux and FreeBSD plus offers upload times equal to or faster than the equivalent GCC with Stlink on the same PC. Forth realtime interactivity also further reduces project development time.

#### Swdcom advantages - Works across a range of the same class of mcu free from usart/pin dependencies. - Fast, faster than a equivalent 460800 Baud serial terminal. - Inbuilt ACK flow control, no compiler ‘choking’ issues. - Clock speed agnostic as long as the MCU clock is running the swd2 terminal functions perfectly. - Uses a single USB cable from the PC to most development boards or a usb/swd programmer for standalone mcus. - Resetting the mcu is easily done by hitting CTRL+C on the PC keyboard.

#### Running this Demo This demo has been tested under Debian-10.6-xfce and FreeBSD so it should work for you on those platforms.

##### FreeBSD Needs the following:

  • xorg

  • xterm

  • git

##### Debian-10.6-xfce Needs the following items and you will need to be root to access the USB devices unless you have those permissions organised on your system:

  • git

  • cmake

  • pkg-config

  • libusb-1.0-0-dev

  • bc

####Runing this demo on a STM32F0 Discovery board 1. Connect a STM32F0 Discovery board to the PC with a USB cable.

  1. Unzip the contents, it will create the “STM32F051K8-SWDCOM” directory.

  2. Cd to the STM32F051K8-SWDCOM directory and open a Xterm.

  3. Compile Swdcom: in the Xterm type “make swdcom” this will build SWD2, the PC terminal client for your system. Swdcom with stlink will be downloaded by Git and built in the demo directory only, nothing is installed outside this directory on your system. It will not interfere with stlink if you have it on your system already.

  4. Flash the Mecrisp-Stellaris binary to the board: in the Xterm type “make flash”. This uses the stlink built in step 4. stlink does not need to be installed on your system.

6. Open a SWD2 FORTH TERMINAL to the STM32F0 Discovery Board: in the Xterm enter “make term”, this will open a new window which will talk to the Board.The SWD2 FORTH TERMINAL window will have this text in the banner “SWD2 FORTH TERMINAL”. Expand this window until it is about half of your screen, with the other half being used by the Xterm window. This is the Forth command line, to test it hit enter a few times and you will get the “ok” acknowledgement, enter “words” and see all the programs already in the system. KEEP the “SWD2 FORTH TERMINAL” OPEN or uploads will FAIL. Watch this window to see the source being uploaded. Although there wont be any errors in the demo, any errors will be displayed in RED text and RING the terminal bell, and ABORT the source code upload.

  1. Upload the library programs: in the Xterm enter either “make lib” or “make 8mhz-lib” and you should see a lot of source scrolling quickly upwards in the “SWD2 FORTH TERMINAL”.

Near the end you will see the following upload statistic showing the Swdcom can upload speed. There are two clock speed options 1. 48 Mhz selected by “make lib” 2. 8 Mhz selected by “make 8mhz-lib” Use this one if “make lib” fails on your board.

<pre> Number of uploaded Words: 317 Source upload time : 32,69 seconds Source upload/compile rate: 9,6 Words per second Hardware: STM32F051 @ 8 MHz

Number of uploaded Words: 299 Source upload time : 6,85 seconds Source upload/compile rate: 43,5 Words per second Hardware: STM32F051 @ 48 mhz </pre>

In the SWD2 FORTH TERMINAL try entering “words4” and you will see a long list of programs.

Finally, in the Xterm enter “make” and you will see project source being rapidly uploaded. Once it has finished it will display a menu.

This concludes the important parts of this demo, namely

  • “make lib” or “make 8mhz-lib” - Rapidly uploading of Forth libraries in their correct sequence to FLASH MEMORY, ending with upload/compile speed statistics.

  • “make” - Rapid upload of Forth project development files to RAM MEMORY for iterative testing/debugging and development.

The SWD2 FORTH TERMINAL menu below lists a few interesting programs to demonstrate project development speed using swdcom on Forth.

<pre>

SWDCOM Demo Main Menu: Press menu key only, all non menu keys ignored

m - Main menu r - print Registers showing CLOCK/FLASH config. Refer to RM0091 Technical Manual for the STM32F051. f - Print MCU memory usage summary b - Blinky. Will blink for 5 seconds then return to the menu w - Print all the programs that are loaded on the MCU h - Help c - Credits l - License q - Quit to Forth prompt. </pre>

Last but not least, there is also a disassembler included. To use it, hit the “q” key to access the Forth prompt. To see the actual assembly code of any Word :

<pre> see greenon 0000CA06: 2090 movs r0 #90 0000CA08: 04C0 lsls r0 r0 #13 0000CA0A: 3080 adds r0 #80 0000CA0C: 0100 lsls r0 r0 #4 0000CA0E: 2380 movs r3 #80 0000CA10: 009B lsls r3 r3 #2 0000CA12: 6183 str r3 [ r0 #18 ] 0000CA14: 4770 bx lr Bytes: 16 ok. </pre>

#### Final notes Everything is controlled by the Makefile and entering “make help” in the Xterm lists all options. Note: some options may require utilities not on your system and previous Forth experience.

<pre> swdcom - compiles the swd2 client for your operating system flash - Flashes the Mecrisp-Stellaris binary to the board term - Opens a Mecrisp-Stellaris Forth terminal to the board lib - Uploads library development files into flash and sets the clock to 48MHz 8mhz-lib - Uploads library development files into flash and sets the clock to 8 MHz all - Uploads project source files and into ram svd - Creates template.xml and will overwrite any existing template.xml edits info - Edits template.cml config file to select desired peripherals for project mem - Modifies memmap.fs and bitfields.fs depending on template.cml config contents mecrisp - Compiles the Mecrisp-Stellaris binary </pre>

#### Swd2 Special Commands

  • (ctrl)+c Reset the Board

  • (ctrl)+$ Upload “upload.fs”

That’s the end of the demo, please note this demo is a full development system. You can add your own files to the project in the Makefile, and then upload and test them simply by entering “make”.

You can use a GUI editor that has a make button, and then a single click will upload your source to the mcu for testing.

Additional peripherals can be added in the svd2forth directory.

## Code cut from main.fs : blinky ( – ) Simple blinky using an accurate Systick delay. .” ———————- “ cr .” Blinking For 4 Seconds “ cr .” ———————- “ cr 10 0 do greenon .” -> “ Hit ctrl+c on the swd2 forth terminal or press the board reset button to stop 200 ms.delay green led 1 second on greenoff blueon .” -> ” 200 ms.delay blue led 0.5 second on blueoff loop ; : fallthru 0 ; : help cr .” —– “ cr .” Help: “ cr .” —– “ cr .” To learn more about Forth, visit: https://mecrisp-stellaris-folkdoc.sourceforge.io/index.html “ cr ; : credits ( – ) cr cr .” ——– “ cr .” Credits: “ cr .” ——– “ cr .” SWDcom Demo written by Terry Porter <terry@tjporter.com.au> “ cr .” SWDcom by Jan Bramkamp <https://github.com/Crest/swdcom> “ cr .” Mecrisp-Stellaris by Matthias Koch https://sourceforge.net/projects/mecrisp/ “ cr ; : license ( – ) cr cr .” ——– “ cr .” License: “ cr .” ——– “ cr .” SWDcom Demo is free software: you can redistribute it and/or modify “ cr .” it under the terms of the GNU General Public License as published by “ cr .” the Free Software Foundation, either version 3 of the License, or “ cr .” (at your option) any later version. “ cr .” “ cr .” This program is distributed in the hope that it will be useful, “ cr .” but WITHOUT ANY WARRANTY; without even the implied warranty of “ cr .” MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the “ cr .” GNU General Public License for more details. “ cr .” “ cr .” You should have received a copy of the GNU General Public License “ cr .” along with this program, see <http://www.gnu.org/licenses/>. “ cr ; : menu-print ( – ) cr cr .” ——————————————————————— “ cr .” SWDCOM Demo Main Menu: Press menu key only, all non menu keys ignored “ cr .” ——————————————————————— “ cr .” m - Main menu “ cr .” r - print Registers showing CLOCK/FLASH config. Refer to RM0091 Technical Manual for the STM32F051.” cr .” f - Print MCU memory usage summary “ cr .” b - Blinky. Will blink for 5 seconds then return to the menu “ cr .” w - Print all the programs that are loaded on the MCU “ cr .” h - Help “ cr .” c - Credits “ cr .” l - License “ cr .” q - Quit to Forth prompt. “ cr cr cr ; : menu ( – ) cr Main Menu menu-print begin key case [char] b of blinky menu-print 0 endof [char] c of credits menu-print 0 endof [char] f of free menu-print 0 endof [char] h of help menu-print 0 endof [char] l of license menu-print 0 endof [char] m of menu-print 0 endof [char] r of prnt menu-print 0 endof [char] w of words4 menu-print 0 endof [char] q of 1 endof fallthru swap default fall thru endcase until cr cr .” You are now at the Forth Prompt; enter ‘menu’ to restart this Menu” cr ; menu