CoreForth-0

cortex/boards/stm32f030-nrf24.ft.md


STM32F030 Board with NRF24 chip

Host

::host::

::dtc:: cr ." *** DTC not supported ***" cr bye

../../common/tether.ft

../janus/compiler.ft

:noname     2drop talign there tlast @ t, tlast ! ; is thead

Target

Board config, hardware setup and cold start

::target::

$08000000 to trom
$20000000 to tram
        4 to tcell

trom tdp !
tram tvp !

tram $00001000 + t, 0 t,
trom $000000d0 + torg

../cpus/cortex-m0/armv6-m-primitives.ft

::stc:: include ../cpus/cortex-m0/threading-stc.ft
::itc:: include ../cpus/cortex-m0/threading-itc.ft
: standby wfi ;

          $FC buffer: psp
             variable s0
          $FC buffer: rsp
             variable r0

          $40 buffer: rxb
             variable rx-head
             variable rx-tail

          $40 buffer: txb
             variable tx-head
             variable tx-tail

../common/core.ft

../dictionary/common.ft

../../common/core.ft

$E000E100 constant NVIC_SETENA_BASE

$48000000 constant GPIOA_MODER
$48000018 constant GPIOA_BSRR
$48000020 constant GPIOA_AFRL
$48000024 constant GPIOA_AFRH
$48000028 constant GPIOA_BRR

$40021000 constant RCC_CR
$40021004 constant RCC_CFGR
$40021014 constant RCC_AHBENR
$40021018 constant RCC_APB2ENR
$40021030 constant RCC_CFGR3

$40013800 constant USART1_CR1
$4001380C constant USART1_BRR
$4001381C constant USART1_ISR
$40013824 constant USART1_RDR
$40013828 constant USART1_TDR

$40013000 constant SPI1_CR1
$40013004 constant SPI1_CR2
$40013008 constant SPI1_SR
$4001300C constant SPI1_DR

$40022000 constant FLASH_ACR

: +led          %10 GPIOA_BSRR ! ;
: -led          %10 GPIOA_BRR ! ;

: +spi          %10000 GPIOA_BRR ! ;
: -spi          %10000 GPIOA_BSRR ! ;

: rbuf-ptr@    @ + c@ ;
: rbuf-ptr!    @ + c! ;
: rbuf-ptr++   dup @ 1+ $3F and swap ! ;

: !txb-empty?    tx-head @ tx-tail @ xor ;
: !rxb-empty?    rx-head @ rx-tail @ xor ;

USART1 Interrupt Handler

Handles USART1 interrupt events:

Both TX and RX use circular buffers with separate head/tail pointers.

i: usart1-handler
                %10000000 USART1_ISR bit@ !txb-empty? and if
                  txb tx-tail rbuf-ptr@ USART1_TDR !
                  tx-tail rbuf-ptr++
                else
                 %10000000 USART1_CR1 bic!
                then

                %00100000 USART1_ISR bit@ if
                  USART1_RDR @ rxb rx-head rbuf-ptr!
                  rx-head rbuf-ptr++
                then
                ;i

Serial Output

Outputs a single character to USART1:

This function implements the standard Forth EMIT word for character output.

: emit          !txb-empty? if begin wfi !txb-empty? not until then
                txb tx-head rbuf-ptr!
                tx-head rbuf-ptr++
                %10000000 USART1_CR1 bis!
                ;

Check Input Available

Checks if there are unread characters in the receive buffer.

Returns true (-1) if there are characters available to read, or false (0) if the receive buffer is empty.

This function implements the standard Forth KEY? word.

: key?          !rxb-empty? ;

Wait for and Read Input Character

Reads a single character from USART1:

This function implements the standard Forth KEY word for character input.

: key           !rxb-empty? not if begin wfi !rxb-empty? until then
                rxb rx-tail rbuf-ptr@
                rx-tail rbuf-ptr++ ;

../../common/output.ft

::tethered:: include ../../common/tether.ft

$C0 constant init-start
$C0 constant init-cold
$C4 constant init-latest
$C8 constant init-dp
$CC constant init-vp

: setup-vars    init-latest @ latest ! init-dp @ (dp) ! init-vp @ vp !
::itc::         true
::stc::         false
                ram? !  ;

::stc:: code reset-handler
::stc::         $4668 $3820 $0006 $4801 $6800 $4687 $00C0 $0000 end-code

::dtc:: code reset-handler
::dtc::         $4668 $3880 $0006 $4f02 $cf20 $1c6d $4728
::dtc::         $ffff $00c0 $0000 end-code


::itc:: t: reset-handler
::itc::         $4668 th, $3880 th, $0006 th, $4f02 th, $cf20 th, $682c th, $46a7 th,
::itc::         $ffff th, $03c0 th, $0000 th,

SPI Data Transfer

Performs a full-duplex SPI data transfer:

This implements the core SPI transaction primitive used by higher-level functions.

: >spi>         begin %10 SPI1_SR bit@ until
                SPI1_DR c!
                begin %1 SPI1_SR bit@ until
                SPI1_DR c@
                ;

: spi>          0 >spi> ;
: >spi          >spi> drop ;

: -rf-ce        ;
: +rf-ce        ;

../../drivers/nrf24.ft

: dump          >r dup cr hex. space begin
                r@ while
                  dup c@ h.2 space 1+
                  r> 1- dup $10 mod 0= over and if cr over hex. space then >r
                repeat rdrop drop ;

Clock Configuration

Configures the system clock to run at 48MHz:

This provides the maximum performance available on STM32F030 devices.

: 48mhz         %1 FLASH_ACR bis!
                %1010000000000000000000 RCC_CFGR bis!
                %1000000000000000000000000 RCC_CR bis!
                %10 RCC_CFGR bis!
                ;

UART1 Configuration

Initializes USART1 for serial communication:

SPI1 Configuration

Initializes SPI1 for communication with external devices (e.g., nRF24L01+):

Host

Finalize binary image and save it

::host::

::dtc:: t' #docol $0C + t@ t' ,enter 8 + t!
::dtc:: t' #dodoes $0C + t@ t' ,dodoes 8 + t!

: vars-to-image [t'] reset-handler 1+  trom $00000004 + t!
                [t'] usart1-handler 1+ trom $000000ac + t!
                [t'] cold              trom $000000C0 + t!
                tlast @                trom $000000C4 + t!
                tdp @                  trom $000000C8 + t!
                tvp @                  trom $000000CC + t!
                ;

: save-bin      ( filename name-len -- )
                vars-to-image
                w/o create-file throw >r
                #target there trom - r@ write-file throw
                r> close-file throw ;

::stc:: s" stm32f030-nrf24-stc.bin" save-bin
::itc:: s" stm32f030-nrf24-itc.bin" save-bin