Author Topic: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?  (Read 1846 times)

0 Members and 1 Guest are viewing this topic.

Offline djsbTopic starter

  • Frequent Contributor
  • **
  • Posts: 893
  • Country: gb
Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« on: March 26, 2023, 06:25:07 pm »
Just started reading about FORTH here

https://mecrisp-stellaris-folkdoc.sourceforge.io/index.html

And I've downloaded both of the Leo Brodie books (Starting FORTH and thinking FORTH-Too big to attach here). Before I go any further down the rabbit hole, is this worth the effort for use with VERY small (I.E very low RAM and Flash memory etc) microcontrollers? I mainly work with PICS, but I've had a go with Micropython (with the Thonny IDE) on the PI PICO. Or has anyone worked with FLASHFORTH on a PIC18F microcontroller?

https://flashforth.com/

Any insights or can anyone share any of their experiences? How easy is it to get an LED blinking on a PIC18F45K50 using FLASHFORTH as an example? Thanks.

P.S Found the SCAMP3 on Tindie (https://www.tindie.com/products/johncatsoulis/scamp/).I think I'll have a go with this first.
« Last Edit: March 26, 2023, 10:06:08 pm by djsb »
David
Hertfordshire,UK
University Electronics Technician, London PIC,CCS C,Arduino,Kicad, Altium Designer,LPKF S103,S62 Operator, Electronics instructor. Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. Credited Kicad French to English translator.
 

Offline iMo

  • Super Contributor
  • ***
  • Posts: 4790
  • Country: pm
  • It's important to try new things..
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #1 on: March 27, 2023, 10:16:40 am »
I was playing a lot with FlashForth (pic18, pic24, dspic33), Mecrisp Forth (stm32, FPGA) and amForth(atmega32/328/128). All are excellent implementations. Also the authors were responsive and collaborative too. Using Forth has sense, indeed, especially with "smaller devices" and FPGAs provided you get quite familiar with the Forth and the way it is written and how it works. That requires to change your programming habits and mindset. Performance-wise those ports are between assembler and C, therefore pretty fast.

Mind the Forth community is rather small, somehow special, the availability of the ready source code or libraries is very limited as well. The existing source code (written by someone else) is difficult to read unless you are quite good at it..  ;)

PS: An example: you may try to debug this 48bit floating point library for Mecrisp I put together years back.. :)
« Last Edit: March 27, 2023, 10:44:44 am by iMo »
 
The following users thanked this post: djsb

Offline rhb

  • Super Contributor
  • ***
  • Posts: 3483
  • Country: us
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #2 on: March 27, 2023, 10:53:03 am »
Mecrisp is superb and is available configured  for many, if not most of the eval boards on the market.

I learned forth on a Vic20 & C64 in the early 80's.  Those implementations were so bad that my only ptogram was a decompiler :-(

Try Mecrisp on an original MSP430 Launchpad.  You can't buy them for the intro price of $4.30, but they are still a great deal if battery life is important.  A friend designed a remote reading system for retrofit to existing meters using an MSP430  with *15 YEAR* battery life!

Have Fun!
Reg
« Last Edit: March 27, 2023, 10:58:23 am by rhb »
 
The following users thanked this post: djsb

Offline sgall17a

  • Newbie
  • Posts: 1
  • Country: au
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #3 on: November 08, 2023, 08:46:12 pm »
I put flashforth on 2 arduino nano clones to control a stepper motor with an A4988 type board in a test jig.  (Speed direction, rotation count etc)
It was interactive and very easy to extend.  Unfortunately I didnt keep a record of my programs. 

The problem was that both nano's become non-functional. I assume that I must have burnt out the flash (by too many overwrites) somehow.  They were cheap clones so maybe the number of writes was pretty limited.  Anyway that was enough for me to move on.

Nevertheless as a concept, forth was really nice as a dedicated control language. I just used text on the serial port but it would have been easy to put some buttons that could excecute a word on  a button press.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4039
  • Country: nz
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #4 on: November 08, 2023, 10:55:26 pm »
is this worth the effort for use with VERY small (I.E very low RAM and Flash memory etc) microcontrollers? I mainly work with PICS, but I've had a go with Micropython (with the Thonny IDE) on the PI PICO

Forth (or some other bytecode such as UCSD Pascal or Java JVM or ...) makes a lot of sense purely in getting maximum functionality into limited code space on primitive CPUs with awful instruction sets and poor code density, as long as you have enough program space to hold the interpreter (or built in words in the case of Forth).

Especially if you are doing a lot with 16 or 32 bit values on an 8 bit CPU.

This certainly includes PIC, as well as 6502 and others of that era, early 1970s minicomputers, 1950s mainframes. Woz's "SWEET16" bytecode interpreter for a 16 bit virtual machine on 6502 was about 300 bytes.

Forth and Micropython also of course have the bonus of providing an interactive console, which can be handy.
 

Offline pqass

  • Frequent Contributor
  • **
  • Posts: 726
  • Country: ca
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #5 on: November 09, 2023, 01:48:11 am »
For a taste of FlashForth 5,
below are the words that I developed to define: standard GPIO, SPI, and stepper functions.

Code: [Select]
[FORTH]$ more atmega328_gpio-base.ff5
-gpio-base
marker -gpio-base
\ gpio ports
#37 constant PORTB \ Port B Data Register
#36 constant DDRB \ Port B Data Direction Register
#35 constant PINB \ Port B Input Pins
#40 constant PORTC \ Port C Data Register
#39 constant DDRC \ Port C Data Direction Register
#38 constant PINC \ Port C Input Pins
#43 constant PORTD \ Port D Data Register
#42 constant DDRD \ Port D Data Direction Register
#41 constant PIND \ Port D Input Pins




[FORTH]$ more atmega328_spi-base.ff5
-spi-base
marker -spi-base
\ spi ports
$4c constant SPCR       \ SPI Control Register
$4d constant SPSR       \ SPI Status Register
$4e constant SPDR       \ SPI Data Register
\ spi bit masks
%000100 constant mSS1   ( PB2 )
%001000 constant mMOSI  ( PB3 )
%010000 constant mMISO  ( PB4 )
%100000 constant mSCK   ( PB5 )
\ spi flags
$80 constant mSPIF           \ SPI Interrupt Flag
$40 constant mWCOL           \ Write Collision Flag
\
: slave.select ( mSSx -- )   \ select slave x in mSSx constant
    dup DDRB  mset           \ SS as output
        PORTB mclr           \ deselect
;
: slave.deselect ( mSSx -- ) \ unselect slave
    dup DDRB  mset           \ SS as output
        PORTB mset           \ deselect
;
%01000000 constant SPCR_SPE  \ SPI Enable
: spi.enable ( ---)
    SPCR_SPE SPCR mset
;
: spi.disable ( ---)
    SPCR_SPE SPCR mclr
;
%00100000 constant SPCR_DORD \ Data Order
: LSB.first ( --- )          \ select LOW Signifiant Bit first
    SPCR_DORD SPCR mset
;
: MSB.first ( --- )          \ select MOST Signifiant Bit first
    SPCR_DORD SPCR mclr
;
%00010000 constant SPCR_MSTR \ Master/Slave Select
: Master.mode
    SPCR_MSTR SPCR mset
;
: Slave.mode
    SPCR_MSTR SPCR mclr
;
\ SPI mode
%00000100 constant SPCR_CPHA  \ Clock Phase
%00001000 constant SPCR_CPOL  \ Clock Polarity
: Mode0
    SPCR_CPHA SPCR mclr       \ Idle CLK = 0
    SPCR_CPOL SPCR mclr       \ Sample on leading edge
;
: Mode1
    SPCR_CPHA SPCR mclr       \ Idle CLK = 0
    SPCR_CPOL SPCR mset       \ Sample on trailing edge
;
: Mode2
    SPCR_CPHA SPCR mset       \ Idle CLK = 1
    SPCR_CPOL SPCR mclr       \ Sample on trailing edge
;
: Mode3
    SPCR_CPHA SPCR mset       \ Idle CLK = 1
    SPCR_CPOL SPCR mset       \ Sample on leading edge
;
\ SPI clock speed
%00000010 constant SPCR_SPR1  \ SPI Clock Rate Selects
%00000001 constant SPCR_SPR0  \ SPI Clock Rate Selects
%00000001 constant SPSR_SPI2x \ Double SPI Speed
: spi2X.off ( --- )
    SPSR_SPI2x SPSR mclr
;
: spi2X.on  ( --- )
    SPSR_SPI2x SPSR mset
;
: fosc/4
    SPCR_SPR1  SPCR mclr
    SPCR_SPR0  SPCR mclr
    spi2X.off
;
: fosc/16
    SPCR_SPR1  SPCR mclr
    SPCR_SPR0  SPCR mset
    spi2X.off
;
: fosc/64
    SPCR_SPR1  SPCR mset
    SPCR_SPR0  SPCR mclr
    spi2X.off
;
: fosc/128
    SPCR_SPR1  SPCR mset
    SPCR_SPR0  SPCR mset
    spi2X.off
;
: spi.init ( -- )
    mSCK DDRB mset      \ SCK as output
    mSCK PORTB mclr     \ clock idles low
    mMOSI DDRB mset     \ MOSI as output
    mMISO DDRB mclr     \ MISO as input
    mMISO PORTB mset    \ activate pull-up on MISO
    mSS1 DDRB mset       \ SS as output
    mSS1 PORTB mset      \ deselect
        spi.enable
        Master.mode
        Mode0
        fosc/64
    SPSR c@ drop SPDR c@ drop \ will clear SPIF
;
: spi.close ( -- )
    $00 SPCR c!
;
: spi.wait ( -- )
    begin
        mSPIF SPSR mtst
    until
;
: spi.cexch ( c1 -- c2 )
    SPDR c! spi.wait SPDR c@
;
: spi.csend ( c1 -- )
    spi.cexch drop
;


[FORTH]$ more atmega328_step-base.ff5
-step-base
marker -step-base
: step.init ( -- )         \ 4 wire, bipolar 293d on PORTC pins 0..3, phases are 0+1, 2+3
$0f DDRC mset ;
: step.off ( -- )
$0 PORTC c! ;
: step.fwd.ms ( ms -- )
$9 PORTC c! dup ms $5 PORTC c! dup ms $6 PORTC c! dup ms $a PORTC c! ms ;
: step.rev.ms ( ms -- )
$a PORTC c! dup ms $6 PORTC c! dup ms $5 PORTC c! dup ms $9 PORTC c! ms ;
: step.fwd.to ( nsteps speed steps -- )                  \ steps/rev rev/s qty
rot rot / swap for dup step.fwd.ms next drop ;
: step.rev.to ( nsteps speed steps -- )                  \ steps/rev rev/s qty
rot rot / swap for dup step.rev.ms next drop ;

Assuming a common anode LED display+74HC595 attached to the standard SCK,MISO,MOSI,SS pins on a ATmega328, then issuing the following will light the display accordingly.
Code: [Select]
spi.init mSS1 slave.deselect $00 spi.csend mSS1 slave.select \ all on
\fed.gcba is segment order
%11111111 spi.csend mSS1 slave.deselect mSS1 slave.select \ all off
%11111000 spi.csend mSS1 slave.deselect mSS1 slave.select \ "7"
%11111001 spi.csend mSS1 slave.deselect mSS1 slave.select \ "1"
%11010000 spi.csend mSS1 slave.deselect mSS1 slave.select \ "3"
%01110001 spi.csend mSS1 slave.deselect mSS1 slave.select \ "4"

Or,
bipolar stepper+L293D driver attached to PORTC pins PC0..3,  then issuing the following will cause it to turn 1000 steps at a particular speed.
Code: [Select]
step.init
200 50 1000 step.fwd.to step.off

I haven't revisited this in 4 years. I just remember that it irked me that every defined word would update the flash. I would have preferred it to write to RAM and only when you're done debugging commit it to flash.  Oh well.
« Last Edit: November 09, 2023, 02:11:36 am by pqass »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Mecrisp Stellaris FORTH or FLASHFORTH-Anyone used them?
« Reply #6 on: November 10, 2023, 04:00:53 am »
Quote
is this worth the effort for use with VERY small (I.E very low RAM and Flash memory etc) microcontrollers?  :I mainly work with PICS, but I've had a go with Micropython (with the Thonny IDE) on the PI PICO. Or has anyone worked with FLASHFORTH on a PIC18F microcontroller?
Forth is pretty neat, but I'm not convinced that any of the microcontrollers you mention really qualify as "VERY small."
PIC18 and Pico all do a pretty good job of supporting more conventional languages (like C and BASIC.)
It's very easy to write nearly unreadable code in Forth-like languages :-(pqass's example was very nice, though!
Since it's interpreted and usually includes an interactive parser, Forth can be very handy for "poking around" during debugging.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf