umouse

umouse.git
git clone git://git.lenczewski.org/umouse.git
Log | Files | Refs | Submodules | README

SysTickDisco072.fth (3662B)


      1 \ SysTickDisco072.fth - Cortex-M0 ticker for STM32F072B-DISCO board
      2 
      3 ((
      4 Copyright (c) 2010, 2011, 2014
      5 MicroProcessor Engineering
      6 133 Hill Lane
      7 Southampton SO15 5AF
      8 England
      9 
     10 tel:   +44 (0)23 8063 1441
     11 fax:   +44 (0)23 8033 9691
     12 email: mpe@mpeforth.com
     13        tech-support@mpeforth.com
     14 web:   http://www.mpeforth.com
     15 Skype: mpe_sfp
     16 
     17 From North America, our telephone and fax numbers are:
     18        011 44 23 8063 1441
     19        011 44 23 8033 9691
     20        901 313 4312 (North American access number to UK office)
     21 
     22 
     23 To do
     24 =====
     25 
     26 Change history
     27 ==============
     28 20140207 SFP003 Converted for STM32F072B-DISCO board.
     29 20110218 MPE002 Added timebase extensions.
     30 ))
     31 
     32 only forth definitions
     33 decimal
     34 
     35 \ ===========
     36 \ *> drivers
     37 \ ===========
     38 \ *S System Ticker
     39 \ ****************
     40 \ *P The system ticker uses the Cortex-M0 *\i{SysTick} timer.
     41 \ ** Although the *\i{SysTick} timer is common to all Cortex-M0
     42 \ ** devices, there is no guarantee that they have the same
     43 \ ** clocking arrangements. Consequently there are different
     44 \ ** drivers for different implementations.
     45 
     46 \ *P The STM32 implementation is fed with the system clock and
     47 \ ** has an option to divide it by 8, controlled by bit 2 of the
     48 \ ** SysTick Control and Status Register (ARM DDI 0337, Rev E,
     49 \ ** page 8-8).
     50 \ *(
     51 \ *B Bit2=0: SysTick clock is HCLK/8.
     52 \ *B Bit2=1: SysTick clock is HCLK.
     53 \ *)
     54 \ *P This code always uses HCLK/8.
     55 
     56 
     57 variable LedActive	\ -- flag
     58 \ *P If *\fo{LedActive} contains non-zero, the ticker is used
     59 \ ** to produce a rotating pattern on the four LEDs.
     60   1 LedActive !
     61 
     62 variable <ticks>
     63 
     64 : ticks		\ -- ms
     65 \ *G Returns a 32 bit number of milliseconds that eventually
     66 \ ** wraps.
     67   <ticks> @
     68 ;
     69 compiler
     70 : ticks		\ -- ms
     71   <ticks> @
     72 ;
     73 target
     74 
     75 PC 6 pio: LDU	\ -- struct
     76 \ *G Red/Upper LED.
     77 PC 9 pio: LDR	\ -- struct
     78 \ *G Green/Right LED.
     79 PC 7 pio: LDD	\ -- struct
     80 \ *G Blue/Down LED.
     81 PC 8 pio: LDL	\ -- struct
     82 \ *G Orange/Left LED.
     83 
     84 : SysTicker	\ --
     85 \ *G Ticker ISR action.
     86   tick-ms <ticks> +!			\ update ticker
     87 \  LedActive @ if
     88 \    case  ticks $7FF and		\ 0..2047
     89 \      $000 of  LDL clrPin  LDU setPin  endof
     90 \      $200 of  LDU clrPin  LDR setPin  endof
     91 \      $400 of  LDR clrPin  LDD setPin  endof
     92 \      $600 of  LDD clrPin  LDL setPin  endof
     93 \    endcase
     94 \  then
     95 ;
     96 ' SysTicker SysTickvec# EXC: SysTickISR	\ -- addr
     97 \ *G Setting the high level ISR.
     98 
     99 tick-ms system-speed 8 / #1000 */ 1- equ /SysTick	\ -- x
    100 \ +G Initial value of the SysTick Reload register.
    101 
    102 internal
    103 \ : initLD	\ struct --
    104 \  dup isOutput setPin
    105 \ ;
    106 external
    107 
    108 : start-clock	\ --
    109 \ *G Initialise the system ticker to run with a period of
    110 \ ** *\fo{tick-ms} milliseconds.
    111 \  LDU initLD  LDR initLD  LDD initLD  LDL initLD
    112   _SCS
    113   /SysTick over stRVR + !		\ set ticker rate
    114   $0003 over stCSR + !			\ enable, bit 0 = enable, bit 1=interrupt
    115 					\ bit 2 = clock source (1=sysclk, 0=sysclk/8)
    116   $FF over $0D23 + c!			\ set SysTick priority to lowest
    117   drop
    118   ei
    119 ;
    120 
    121 : stop-clock	\ --
    122 \ *G Stop the sytem ticker.
    123   bit0 _SCS stCSR + bic!  ;
    124 
    125 : later         \ n -- n'
    126 \ *G Generates the timebase value for termination in
    127 \ ** n millseconds time.
    128   ticks +
    129 ;
    130 
    131 : timedout?     \ n -- flag ; true if timed out
    132 \ *G Flag is returned true if the timebase value n has timed out.
    133 \ ** *\fo{TIMEDOUT?} does not call *\fo{PAUSE}.
    134   ticks - 0<                            \ must use circular arithmetic
    135 ;
    136 
    137 compiler
    138 : later         \ n -- n'
    139   ticks +
    140 ;
    141 
    142 : timedout?     \ n -- flag ; true if timed out
    143   ticks - 0<                            \ must use circular arithmetic
    144 ;
    145 target
    146 
    147 : ms            \ n --
    148 \ *G Waits for n milliseconds.
    149   later begin
    150     pause  dup timedout?
    151   until
    152   drop
    153 ;
    154 
    155 
    156 \ ======
    157 \ *> ###
    158 \ ======
    159 
    160 decimal
    161