Forth Sourcecode Dependency Generation

Project: svd2forth-v6-f0disco-blinky
Created: Tue 15 Jun 2021 16:50:59 AEST
Author Copyright 2021 by t.j.porter <terry@tjporter.com.au>
Purpose: Forth Sourcecode Dependency Generation tested with a Blinky
Intended Audience: Forth users
MCU: STM32F051
Board: F0 Disco
Core: Mecrisp-Stellaris
Required: svd2forth-v6 with Gema pre-processor http://gema.sourceforge.net/new/index.shtml
Recommended:
Critical:
Based on:
References:
svd2forth-v6-f0disco-blinky URL:
license: MIT, please see COPYING

_images/compiler.jpg

Description

This article demonstrates generating Fourth memory map and bitfield dependencies automatically from the sourcecode. A Blinky is used to demonstrate the process.

About three weeks ago I published an article here called “Obsoleting the Memory Map” which was the first test of this concept. This replaced Forth constants with hex addresses for the RP2040 mcu.

As you can see, the ‘Source File’ below is a simple F0 Discovery blinky program and uses a ms.delay preloaded in flash along with my usual development files.

What IS NOT loaded into the 64KB flash are the memory map and bitfield dependencies which are far too big at 122KB.

The STM32F051 on chip forth compiler obviously needs to know what ‘RCC_AHBENR_IOPCEN’ is, along with ‘RCC_AHBENR’ and other Blinky Words, but they aren’t in the ‘Source File’ or elsewhere, so what’s happening ?

Gema is preprocessing the ‘Source File’ and creating a dependency file using memory map and bitfield files created by SVD2FORTH-V6. This dependency file is named ‘includes.fs’ and is uploaded after the ‘config.fs’ file and before the ‘Source File’. You can see the sequence in ‘Intermediate File’ below.

Finally spaces and comments are stripped and the code is uploaded to the STM32F051 on chip forth compiler as shown in ‘Uploaded File’ below.

This is all orchestrated by a Makefile, I type ‘make’ and the led blinks :)

Bitfield Browser

Now what to do for a easy to use ‘bitfield browser’ ? Something is needed that’s easy to use for searching, scrolling and clipboard pasting. It needs to show the bitfield name, stack comment, actions, offset and bitwidth and finally the STmicro bitfield description.

One can get all this stuff from the Technical Manual but I want to see if there is a way to have a smaller window application that’s easy to use, something that fits into my already crowded development environment screen along with the Swdcom terminal, xterm and Gvim editor.

_images/gema-dev-env.jpg

I’m thinking of building a Sqlite database accessed by a Perl/GTK gui front end.

I did try various ways to generate HTML using XSLT but no joy because my Browsers are huge, slow and flaky. I’m learning to hate HTML. I may try again using the Dillo browser, which while lacking any Java* support is fast and reliable.

Source File

projfile.fs The sourcefile. This is the simple blinky to test everything works without any hand crafted dependencies.

\ ---------------------------------------------------------------------------\
\ Program Name: svd2forth-v6-f0disco-blinky.fs
\ Date: Tue 15 Jun 2021 16:51:24 AEST
\ Copyright 2021 by t.j.porter <terry@tjporter.com.au>,  MIT Licensed
\ For Mecrisp-Stellaris by Matthias Koch. \ https://sourceforge.net/projects/mecrisp/
\ Chip: STM32F051
\ Board: STM32F0 Discovery Board
\ All register names are CMSIS-SVD compliant
\ Short Program Description: Forth Sourcecode Dependency Generation tested with a Blinky
\ See doc/README.html for full description
\ User LD3: Green user LED connected to PC-9
\ User LD4: Blue user LED connected to PC-8


\ Enable Port-C
RCC_AHBENR_IOPCEN RCC_AHBENR bis!


\ Set PC9 amd PC8 as output
OUTPUT GPIOC_MODER_MODER9<< OUTPUT GPIOC_MODER_MODER8<<
                                  +   GPIOC_MODER bis!

\ Create On and Off for Green and Blue user LEDs
: greenon ( -- ) GPIOC_BSRR_BS9! ;
: greenoff ( -- ) GPIOC_BSRR_BR9! ;
: blueon ( -- ) GPIOC_BSRR_BS8! ;
: blueoff ( -- ) GPIOC_BSRR_BR8! ;

\ one second delay
: waitone  ( -- )
  1000 ms.delay
;

\ Turn on Green, off Blue ...
: blinking  ( -- )
begin
   greenon blueoff waitone
   greenoff blueon waitone
again
;

blinking

Intermediate File

After automatic pre-processing, these three files are concatenated and fed to the final stage, where comments and blank lines are stripped before uploading to the on chip Forth.

config.fs

This is my standard config file:

  1. init.calltrace: Stops the dreaded ‘Unhandled Interrupt 00000003 !’ message from scrolling endlessly up the terminal and tells me what caused it.

  2. 48mhz: sets the clock to 48 Mhz, the maximum for this Cortex-M0 mcu.

  3. 48000 init.systick: sets the Systick to 1 millisecond ticks and provides the ‘ms.delay’ blocking delay.

\ ------------------------------------------------------------------------------ \
\ config.fs
\ purpose: set up initial conditions

compiletoram

: init ( -- )
   init.calltrace
   48mhz
   48000 init.systick     \ 1ms increments
;

init

includes.fs

This file containing memory maps and bitfield definitions is created by the Gema pre-processor which has scanned the Source File and included all dependencies needed. These are sourced from the SVD2FORTH created memmap and bitfield pattern files.

\ ------------------------------------------------------------------------------ \
\ includes.fs
\ preprocessed dependencies from svd2forth-v6-f0disco-blinky
compiletoram
$40021014 constant RCC_AHBENR \ AHB Peripheral Clock enable register
$48000800 constant GPIOC_MODER \ GPIO port mode register
$48000818 constant GPIOC_BSRR \ GPIO port bit set/reset  register
: GPIOC_BSRR_BR8!  ( -- ) 1 24 lshift GPIOC_BSRR !  ; \ wo, bitWidth:1 bitOffset:24   Port x reset bit y y =  0..15
: GPIOC_BSRR_BR9!  ( -- ) 1 25 lshift GPIOC_BSRR !  ; \ wo, bitWidth:1 bitOffset:25   Port x reset bit y y =  0..15
: GPIOC_BSRR_BS8!  ( -- ) 1 8 lshift GPIOC_BSRR !  ; \ wo, bitWidth:1 bitOffset:8   Port x set bit y y=  0..15
: GPIOC_BSRR_BS9!  ( -- ) 1 9 lshift GPIOC_BSRR !  ; \ wo, bitWidth:1 bitOffset:9   Port x set bit y y=  0..15
: GPIOC_MODER_MODER8<< ( %bb -- x ) 16 lshift  ; \ rw, bitWidth:2 bitOffset:16   Port x configuration bits y =  0..15
: GPIOC_MODER_MODER9<< ( %bb -- x ) 18 lshift  ; \ rw, bitWidth:2 bitOffset:18   Port x configuration bits y =  0..15
: RCC_AHBENR_IOPCEN ( -- x )  1 19 lshift  ; \ rw, bitWidth:1 bitOffset:19   I/O port C clock enable

projfile.fs

The sourcefile. This is the simple Blinky to test everything works.

\ ---------------------------------------------------------------------------\
\ Program Name: svd2forth-v6-f0disco-blinky.fs
\ Date: Tue 15 Jun 2021 16:51:24 AEST
\ Copyright 2021 by t.j.porter <terry@tjporter.com.au>,  MIT Licensed
\ For Mecrisp-Stellaris by Matthias Koch. \ https://sourceforge.net/projects/mecrisp/
\ Chip: STM32F051
\ Board: STM32F0 Discovery Board
\ All register names are CMSIS-SVD compliant
\ Short Program Description: Forth Sourcecode Dependency Generation tested with a Blinky
\ See doc/README.html for full description
\ User LD3: Green user LED connected to PC-9
\ User LD4: Blue user LED connected to PC-8


\ Enable Port-C
RCC_AHBENR_IOPCEN RCC_AHBENR bis!


\ Set PC9 amd PC8 as output
OUTPUT GPIOC_MODER_MODER9<< OUTPUT GPIOC_MODER_MODER8<<
                                  +   GPIOC_MODER bis!

\ Create On and Off for Green and Blue user LEDs
: greenon ( -- ) GPIOC_BSRR_BS9! ;
: greenoff ( -- ) GPIOC_BSRR_BR9! ;
: blueon ( -- ) GPIOC_BSRR_BS8! ;
: blueoff ( -- ) GPIOC_BSRR_BR8! ;

\ one second delay
: waitone  ( -- )
  1000 ms.delay
;

\ Turn on Green, off Blue ...
: blinking  ( -- )
begin
   greenon blueoff waitone
   greenoff blueon waitone
again
;

blinking

Uploaded File

After automatic comment and blank line removal this is uploaded to the on chip Forth.

compiletoram
: init
   init.calltrace
   48mhz
   48000 init.systick
;
init
compiletoram
$40021014 constant RCC_AHBENR
$48000800 constant GPIOC_MODER
$48000818 constant GPIOC_BSRR
: GPIOC_BSRR_BR8!   1 24 lshift GPIOC_BSRR !  ;
: GPIOC_BSRR_BR9!   1 25 lshift GPIOC_BSRR !  ;
: GPIOC_BSRR_BS8!   1 8 lshift GPIOC_BSRR !  ;
: GPIOC_BSRR_BS9!   1 9 lshift GPIOC_BSRR !  ;
: GPIOC_MODER_MODER8<<  16 lshift  ;
: GPIOC_MODER_MODER9<<  18 lshift  ;
: RCC_AHBENR_IOPCEN   1 19 lshift  ;
RCC_AHBENR_IOPCEN RCC_AHBENR bis!
OUTPUT GPIOC_MODER_MODER9<< OUTPUT GPIOC_MODER_MODER8<<
                                  +   GPIOC_MODER bis!
: greenon  GPIOC_BSRR_BS9! ;
: greenoff  GPIOC_BSRR_BR9! ;
: blueon  GPIOC_BSRR_BS8! ;
: blueoff  GPIOC_BSRR_BR8! ;
: waitone
  1000 ms.delay
;
: blinking
begin
   greenon blueoff waitone
   greenoff blueon waitone
again
;
blinking

The LED Is Now Blinking

Just the source, no handcrafted dependencies are needed thanks to Gema!