Systick Library

This is a general purpose interrupt driven Systick timing library that should work on any STM32Fxxxx.

It has an accurate blocking delay and event timer.

Note

Systick must be initialised before use and takes the “1ms-cal-value” as a parameter: “<1ms-cal-value> init.systick”

Library Words

Word

Description

Example

ms.delay ( u - - delay )

Accurate BLOCKING ms delay

1000 ms.delay

ms.counter.reset ( - - )

zero counter

ms.counter.reset

ms.read ( - - )

put ms.counter value on Stack

1000 on the stack

ms.print ( - - )

print ms.counter to terminal screen

1000 ms

seconds.print ( - - )

print seconds to 3 decimal places

Elapsed time is: 1.000 seconds

ms.reached? ( u - - yes = -1 | no = 0)

True if count >= u

500 ms.reached?

Code

Download systick.fs

~/projects/programming-languages/forth/mecrisp-stellaris/systick/systick.fs.html
  1 \ Program Name: systick.fs
  2 \ Date: Wed 8 Jan 2020 13:13:57 AEDT
  3 \ Copyright 2020 by t.j.porter <terry@tjporter.com.au>,  licensed under the GPLV3
  4 \ For Mecrisp-Stellaris by Matthias Koch.
  5 \ https://sourceforge.net/projects/mecrisp/
  6 \ Standalone: no preloaded support files required
  7 \
  8 \ This Program: Interrupt driven STM32Fxxx Systick Timing Library
  9 \
 10 \ Note the (STK) is not a part of STM32F CMSIS-SVD
 11 \ See: ST PM0215 Programming manual, page 85-91
 12 \
 13 \ --------------------------- Example 1ms-cal-value -------------------------\
 14 \  8000   Standard Mecrisp-Stellaris using 8MHz internal RC clock
 15 \  48000  STM32F0xx running at 48 MHz
 16 \  72000  STM32F10x running at 72 MHz
 17 \ ------------------------- Example Library Usage ---------------------------\
 18 \
 19 \  Systick must be initialised before use: "<1ms-cal-value> init.systick"
 20 \
 21 \ ---------------------------------------------------------------------------\
 22 \ 
 23 \  : 10s-delay cr          \ Demo 10 second delay
 24 \    ." 10 second delay starting, please wait ..." cr
 25 \    10000 ms.delay   ( 10 seconds )
 26 \    ." 10 second delay finished" cr
 27 \  ; 
 28 \   
 29 \  : do.stuff ( -- )       \ blocking do-loop  
 30 \    4800000 0 do loop     \ time will vary depending on interrupts etc so
 31 \  ;                       \ blocking do-loop are not very accurate
 32 \    
 33 \  : timeit cr             \ measure the time taken to "do.stuff" above
 34 \    ms.counter.reset
 35 \    do.stuff
 36 \    ." do.stuff took "
 37 \    ms.print cr
 38 \  ;
 39 \  
 40 \  10s-delay
 41 \ 
 42 \  timeit 
 43 \  do.stuff took 184 ms
 44 \
 45 \ ---------------------------------------------------------------------------\
 46 
 47  compiletoflash
 48  \ compiletoram
 49 
 50  $E000E010   constant stk_csr     \ RW  SysTick control and status  
 51  $E000E014   constant stk_rvr     \ RW  SysTick reload value 
 52  $E000E018   constant stk_cvr     \ RW  SysTick current value 
 53  $E000E01C   constant stk_calib   \ RO  SysTick calibration value 
 54 
 55  0 variable ms.counter  \ 32 bits or -> $ffffffff u. = 4294967295 ms, 4294967 seconds,
 56                         \ 71582 minutes, 19.88 Hrs
 57 
 58  : tickint ( -- )       \ tickint: sysTick exception request enable
 59    %010 stk_csr bis!    \ 1 = Counting down to zero asserts the SysTick exception request.
 60  ;
 61 
 62  : systick-handler ( -- )
 63    1 ms.counter +!
 64  ;
 65 
 66  : init.systick ( 1ms-cal-value -- )   \ init systick
 67    stk_rvr !                           \ systick calib for 1ms 
 68    %101 stk_csr bis!                   \ systick enable
 69    ['] systick-handler irq-systick !   \ 'hooks' the systick-handler word (above) to the systick interrupt
 70    tickint
 71  ;
 72 
 73  : ms.counter.reset ( -- )    \ clear the ms.counter
 74    0 ms.counter !
 75  ;
 76 
 77  : ms.read ( -- )             \ push elapsed ms since ms.counter.reset to TOS (top of stack)
 78    ms.counter @               \ Use for computations
 79  ;
 80 
 81  : ms.print ( -- )            \ print elapsed time in ms since ms.counter.reset
 82    ms.read u. ." ms" cr
 83  ;
 84 
 85  : seconds.print ( -- )       \ print elapsed time in seconds to three decinal places since ms.counter.reset
 86    ." Elapsed time is: "
 87    ms.read 0 <#  # # # 46 hold #s #> type
 88   ."  seconds " cr
 89  ;
 90 
 91  : ms.reached? ( u -- yes = -1 | no = 0 )     \ TRUE when u reached
 92    ms.read u<
 93  ;
 94 
 95  : ms.delay ( u -- )  \ accurate  blocking delay, range is 1ms to 19.88 Hrs (32 bytes)
 96    ms.counter.reset
 97       begin
 98          ms.read over u>=
 99       until
100    drop
101  ;
102 
103 \ 72000 init.systick   \ Add to any existing init word to run at boot from Flash.
104  compiletoram