CoreForth-0

avr/boards/produino-uno-4u.ft.md


Arduino CoreForth

CoreForth-0 for the Produino Uno 4u using the AVR128DA28 MCU. Configured to run at 24MHz, with UART at 115200bps. LED at PC2 (D13) functionality.

::host::

cr

ROM starts at 0x0000 and RAM at 0x4000. ROM is also mapped at 0x8000 for LD/ST instructions.

$00000000 to trom
$00004000 to tram

trom tdp !
tram tvp !

Start at 0x110 to leave space for system variables at 0x100

$110 torg

::itc:: include ../janus/host-itc-avr.ft
::itc:: include ../janus/target-itc-avr.ft
::stc:: include ../janus/host-stc-avr.ft
::stc:: include ../janus/target-stc-avr.ft

::target::

System variables

$8100 constant  init-latest
$8102 constant  init-dp

$7fff constant  r0
$7fbf constant  s0

Include the rest of the system:

../periph-avr.ft

../memory-avr.ft

../core.ft

: emit          begin USART0.STATUS c@ %00100000 and until USART0.TXDATAL c! ;
: key?          USART0.STATUS c@ %10000000 and ;
: key           begin key? until USART0.RXDATAL c@ ;

../../common/exception.ft

../../common/timeout.ft

../../common/core.ft

../../common/output.ft

../../common/input.ft

../dictionary.ft

../utils.ft

::itc:: include ../compiler-itc.ft
::stc:: include ../compiler-stc.ft

../../common/control-flow.ft

../../common/interpret.ft

Hardware initialization

Set the UART to 115200bps at 24MHz (CLK_PER at 12MHz)

: uart-init    #416 USART0.BAUD !
               %00000011 USART0.CTRLC c!
               %00000001 PORTA.DIRSET c!
               %00000010 PORTA.DIRCLR c! 
               %11000000 USART0.CTRLB c! ;

LED at PC2 (D13)

: led-init     %00000100 PORTC.DIRSET c! ;
: +led         %00000100 PORTC.OUTSET c! ;
: ~led         %00000100 PORTC.OUTCLR c! ;
: -led         %00000100 PORTC.OUTTGL c! ;

SPI

: +spi         %00001000 PORTC.OUTCLR c! ;
: -spi         %00001000 PORTC.OUTSET c! ;
: spi-init     %00001101 PORTC.DIRSET c!
               %00100001 SPI1.CTRLA c!
               %00000000 SPI1.CTRLB c!
               -spi ;
: >spi>        SPI1.DATA c! begin SPI1.INTFLAGS c@ %10000000 and until SPI1.DATA c@ ; 
: >spi         >spi> drop ;
: spi>         $ff >spi> ;

NRF24

: rf-ce-init   %10000000 PORTD.DIRSET c! ;
: -rf-ce       %10000000 PORTD.OUTCLR c! ;
: +rf-ce       %10000000 PORTD.OUTSET c! ;
: rf-irq?      PORTD.IN c@ %01000000 and ;

Initialize the UART and LED

: hw-init      $00 NVMCTRL.CTRLB c!
               %00000001 CLKCTRL.MCLKCTRLB ioreg-ccp!
               %00100100 CLKCTRL.OSCHFCTRLA ioreg-ccp!
               uart-init
               led-init
               ;

: setup-pointers
               init-latest @ latest !
               init-dp @ dp !
               ;

Declare a default application word which starts the interpreter. This can be overridden in files loaded as extras:

: turnkey      0 (source-id) !
::itc::        ." CoreForth-0 AVR ITC ready" cr
::stc::        ." CoreForth-0 AVR STC ready" cr
               hex
               quit
               ;

Include any extra files:

include-extras

Cold start of the system:

: cold         hw-init
               setup-pointers
               turnkey
               reset ;

Finalize the compilation back in host mode:

::host::

Set the IRQ handler for reset

 #1 t' reset-handler irq-handler!

Store the values for latest and here (using the latest allocated address in RAM), and let execution start with cold:

    tlast @ $8000 or $100 >target w!
               tvp @ $102 >target w!
::itc:: t' cold $8000 or t' start tcell + >target w!
::stc:: t' cold t' start tcell + >target w!

Clear the first word’s link pointer to finish the dictionary linked list:

0 $110 >target w!

Finally, write out the Intel HEX file with the compiled program:

::itc:: s" produino-uno-4u-itc.hex" write-hex
::stc:: s" produino-uno-4u-stc.hex" write-hex