Wellys Dev

  • Home
  • Search/Topics
  • Writings
  • About

    2023

  • 2023-04-05
    Mecrisp-Stellaris Forth: An IDE for the RP2040

    Where I describe how I develop Forth on my Mac using Sublime Text 3 and Serial, as my Forth IDE.

    Summary

    1. Edit Forth code in Sublime Text 4.
    2. Execute ST’s Build command which transfers the code to the board for on-board compiling for Forth, typically a 1-2 second transfer (1500 bytes/sec).
    3. Use the macOS app, Serial for communicating with Forth on the board.

    Sources

    • FlashForth Shell
    • Piotr’s xfr for Mecrisp-Stellaris Forth
    • Jan’s upload.c for Mecrisp-Stellaris Forth
    • Terry’s IDE Notes

    Install Forth on RP2040

    1. Setup

    Increase Baud Rate

    Make the change in: mecrisp-stellaris-x.y.z/mecrisp-stellaris-source/rp2040-ra/terminal.s lines 165-166 and added the comment below the lines, replacing the comment line 168:

  • 2022

  • 2022-05-06
    Mecrisp-Stellaris Forth: Dictionary 0

    Where I keep my dictionary 0 with all of the basic Forth definitions.

    Introduction

    It helps to have the complete dictionary for Mecrisp-Stellaris Forth, this is dictionary 0 which has my most basic definitions. I load this before I start doing any work in Forth on the RP2040.

    \ main dictionary for words which have been debugged
    compiletoflash
    : words4 ( -- ) cr  \ A columnar Word list printer. Width = 20 characters, handles overlength Words neatly 
       0				    \ column counter
       dictionarystart
          begin
    	 dup 6 + dup
    	 ctype			    \ dup before 6 is for dictionarynext input
    	 count nip		    \ get number of characters in the word and drop the address of the word
    	     20 swap - dup 0 > if   \ if Word is less than 20 chars
    		   spaces swap	    \ pad with spaces to equal 20 chars
    		   else drop cr	    \ issue immediate carriage return and drop negative number
    		   nip -1	    \ and reset to column -1
    		   then		       
    		      dup 3 = if 3 - cr \ if at 4th column, zero column counter			   
    		      else 1 +
    		      then
    	 swap		       
    	 dictionarynext		    \ 	( a-addr - - a-addr flag )
          until
       2drop
    ;
    
    : freememory ( -- )
    	compiletoflash unused ." FLASH: " .
    	compiletoram unused ." RAM: " .
    ;
    
    \ xterm colors 256!
    \ https://github.com/sindresorhus/xterm-colors
    : esc 27 emit ;
    : black      ( -- cursor colour )  esc ." [38;5;0m" ;
    : red        ( -- cursor colour )  esc ." [38;5;9m" ;
    : green      ( -- cursor colour )  esc ." [38;5;2m" ;
    : purple     ( -- cursor colour )  esc ." [38;5;93m" ;
    : blue       ( -- cursor colour )  esc ." [38;5;12m" ;
    : magenta    ( -- cursor colour )  esc ." [38;5;127m" ;
    : cyan       ( -- cursor colour )  esc ." [38;5;51m" ;
    : white      ( -- cursor colour )  esc ." [38;5;15m" ;
    : grey       ( -- cursor colour )  esc ." [38;5;8m" ;
    : fuchsia    ( -- cursor colour )  esc ." [38;5;13m" ;
    : green3     ( -- cursor colour )  esc ." [38;5;34m" ;
    : lime       ( -- cursor colour )  esc ." [38;5;10m" ;
    : navy       ( -- cursor colour )  esc ." [38;5;4m" ;
    : darkorange ( -- cursor colour )  esc ." [38;5;208m" ;
    : grey62     ( -- cursor colour )  esc ." [38;5;247m" ;
    : grey82     ( -- cursor colour )  esc ." [38;5;252m" ;
    
    : test_black      black      ." BLACK black " black ;
    : test_red        red        ." RED red " black ;
    : test_green      green      ." GREEN green " black ;
    : test_purple     purple     ." PURPLE purple " black ;
    : test_blue       blue       ." BLUE blue " black ;
    : test_magenta    magenta    ." MAGENTA magenta " black ;
    : test_cyan       cyan       ." CYAN cyan" black ;
    : test_white      white      ." WHITE white " black ;
    : test_grey       grey       ." GREY grey " black ;
    : test_fuchsia    fuchsia    ." FUCHSIA fuchsia " black ;
    : test_green3     green3     ." GREEN3 green3 " black ;
    : test_lime       lime       ." LIME lime " black ;
    : test_navy       navy       ." NAVY navy " black ;
    : test_darkorange darkorange ." DARKORANGE darkorange " black ;
    : test_grey62     grey62     ." GREY62 grey62 " black ;
    : test_grey82     grey82     ." GREY82 grey82 " black ;
    
    : colors 
            cr test_black cr test_grey cr test_grey62 cr test_grey82 
            cr test_white cr test_red cr test_darkorange cr test_lime
            cr test_green3 cr test_green cr test_cyan cr test_blue
            cr test_navy cr test_magenta cr test_fuchsia cr test_purple
            cr
    ;
    
    : bp blue cr . .s cr black ;
    
    $40014000 			constant IO_BANK0_GPIO0_STATUS \ GPIO status
    $40014004 			constant IO_BANK0_GPIO0_CTRL \ GPIO control including function select and overrides.
    $d0000000 			constant SIO_BASE
    
    #5 			    constant SIO 				\ SIO (F5) DS_p258
    SIO_BASE $004 + constant GPIO_IN       \ Input value for GPIO
    SIO_BASE $010 + constant GPIO_OUT      \ GPIO output value
    SIO_BASE $014 + constant GPIO_OUT_SET  \ GPIO output value set
    SIO_BASE $018 + constant GPIO_OUT_CLR  \ GPIO output value clear
    SIO_BASE $01c + constant GPIO_OUT_XOR  \ GPIO output value XOR
    SIO_BASE $020 + constant GPIO_OE       \ GPIO output enable
    SIO_BASE $024 + constant GPIO_OE_SET   \ GPIO output enable set
    SIO_BASE $028 + constant GPIO_OE_CLR   \ GPIO output enable clear
    SIO_BASE $02c + constant GPIO_OE_XOR   \ GPIO output enable XOR
    
    \ Feather RP2040 localization
    #13					constant GP13
    GP13      			constant LED
    #2						constant minGPIO
    #29					constant maxGPIO
    #0						constant minTest
    #3						constant maxTest
    #8						constant padsize
    
    : GPIO_ctrl ( GPIO -- ) \ get the address for the specific GPIO ctrl register
    	#8 * IO_BANK0_GPIO0_CTRL +
    ;
    
    \ print values of the GPIO_CTRL registers of all GPIO pins
    : .CTRL ( -- ) \ print CTRL values of all GPIO pins
    	30 0 CR DO 
    	I GPIO_ctrl @
    	I . . CR
    	LOOP 
    ;
    
    : one_sec ( -- ) 		\ one sec ( -- )ond delay
    	1000 ms
    ;
    
    : half_sec ( -- )		\ half sec ( -- )ond delay
    	500 ms
    ;
    
    : qtr_sec ( -- )		\ quarter sec ( -- )ond delay
    	250 ms
    ;
    
    : tenth_sec ( -- )		\ tenth sec ( -- )ond delay
    	100 ms
    ;
    
    : GPIO_F5 ( GPIO -- )		\ ensure GPIO is in F5
    	dup GPIO_ctrl 
    	@ %11111 and
    	5 = if drop else ." Not F5! " 5 swap GPIO_ctrl ! then
    ;
    
    : GPIO_OUT ( GPIO -- )	\ set GPIO to output, uses atomic set
    	1 swap lshift GPIO_OE_SET !
    ;
    
    : tog_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_XOR !
    ;
    
    : high_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_SET !
    ;
    
    : low_GPIO ( GPIO -- )	
    	1 swap lshift GPIO_OUT_CLR !
    ;
    
    : blink_GPIO ( GPIO -- ) 
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    tenth_sec ( -- )
    	  key? until drop
    ; 
    
    : ms_blink_GPIO ( n GPIO -- ) \ blink GPIO every n milliseconds, until key
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    swap dup ms swap
    	  key? until drop drop
    ; 
    
    : us_blink_GPIO ( n GPIO -- ) \ blink GPIO every n microseconds, infinite
    	dup GPIO_F5
    	dup GPIO_OUT 
    	  begin
    	    dup tog_GPIO
    	    swap dup us swap
    	  again drop drop
    ; 
    
    padsize buffer: pad
    : .pad 
    	padsize 0 do 
       pad I + c@ hex .
       loop
    ;
    
    : erase_pad 
    	padsize 0 do 
       0 pad I + c! 
       loop 
    ;
    
    : endofDict0 ;
    0 save#
    compiletoram
    
  • 2021

  • 2021-07-09
    Mecrisp-Stellaris Forth: Creating a New UF2

    Where I describe how to edit, assemble, link, and UF2 Mecrisp-Stellaris for the RP2040 and specifically, the Adafruit Feather RP2040 and Pico/Pico W.

    Sources

    • Inspiration - Piotr’s Comment
    • Mecrisp Patching UserDocs

    Updated

    Now that I have gone through over 20 assemble/link/UF2 cycles, I have a better understanding of what needs to be done, specific to macOS Big Sur.

    1. Follow the comments as to “The process…” particularly as it regards getting macOS to run the ARM toolchain. This has to happen first.
    2. Reduce the complexity of Mecrisp-Stellaris. Matthias has performed a remarkable job in getting so many processors to run this version of Forth. This achievement needs to be applauded and appreciated. That said, unless you are running a significant number of versions, delete those that you aren’t using. In my case, I used the 128k/128K version to determine what was required for the RP2040. This is my base moving forward. Note In reviewing version 2.6.5, the new model is 128K/128K from Matthias.
    3. Terry’s directions below (Understatement), however, its even easier. If you have performed point 2, then the solution is simply:
    cd mecrisp-stellaris-x.x.x
    ./release
    # put board in USB mode, drag/drop UF2 file and go to work
    

    Really important things to do: (Instructions below)

    1. Implement Terry’s improvement on Stair-Stepping by adding a CR to the OK prompt.
    2. Implement Piotr’s improvement on uploading

    Understatement

    (With apologies to Terry Porter) Terry’s page above as Mecrisp Patching was extremely helpful and contained this nugget. Which is true, however, it takes a little bit of additional work to get there with the RP2040.

  • 2021-07-05
    Mecrisp-Stellaris Forth: RP2040 and Pin Testing

    Where I work with the Adafruit Feather RP2040 (Feather), Mecrisp-Stellaris Forth (MSForth) and create Forth versions of ManPinTest and PinTest.

    Sources

    • Adafruit Feather RP2040
    • Raspberry Pi RP2040 Getting Started
    • RP2040 Datasheet
    • Arm: Raspberry Pi RP2040: Our Microcontroller for the Masses
    Adafruit Feather RP2040

    Background

    This entry will be very similar to the one using the RP2040 and MicroPython, the difference will is I will use MSForth instead.

    Getting Started

    I’m going to assume that the serial connection, Forth installation and the ability to write/edit/run Forth programs on the RP2040 already exists. If not see this entry.

  • 2021-06-27
    Mecrisp-Stellaris Forth: Notes

    Where I compile interesting notes as to how Mecrisp-Stellaris Forth is implemented on the RP2040 microcontroller.

    ALWAYS WRITE SHORT DEFINITIONS!!

    I was working on this entry and was attempting to hand-write the definition of PIN_BLINK by keeping the stack in my head. As my definition approached over 10 albeit short lines, it continued to not work. This is when I remembered, Forth is designed to be easy to read and interactive, work with Forth, not against it!

  • 2021-06-23
    Mecrisp-Stellaris Forth: On the RP2040

    Where I implement Forth on the Feather RP2040 using Mecrisp-Stellaris Forth.

    Sources

    • Mecrisp Forth
    • Mecrisp Forth Glossary
    • Mecrisp Forth Dictionary
    • Mecrisp Downloads
    • Mecrisp-Stellaris Unofficial User Documentation
    • Mecrisp General Discussion
    • Pi Pico Memory Map for Forth
    • Run Forth on Pico Video

    Products

    • Raspberry Pi Pico W
    • Adafruit Feather RP2040
    • USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi
    • Lithium Ion Polymer Battery - 3.7v 1200mAh

    How to Begin

    (Follow directions based on your board)

  • 2021-03-21
    Why Forth?

    As I got back into Forth after about 35 years of other languages, programs and technical stuff, I’ve been asked, Why Forth?

    The most personal reason is that I’ve had a life-long passion for the language and kept a bookshelf full of Forth books for over 40 years. However, once I began to use it again, I realized its perfect for embedded processors. I’m surprised how well it works with today’s microcontrollers. And how relatively easy it is to find a port of Forth for many popular microcontrollers. I still think its a good question to ask, so I start this blog with the question:

Copyright © 2025 Lief Koepsel
  • Home
  • Search/Topics
  • Writings
  • About