umouse

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

FlashSTM32.fth (3910B)


      1 \ FlashSTM32.fth -
      2 
      3 ((
      4 Copyright (c) 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 ))
     29 
     30 \ *************************
     31 \ *! flashstm32
     32 \ *T STM32 Flash operations
     33 \ *************************
     34 \ *P Although some of these routines are not the most efficient
     35 \ ** for the STM32 series, they are designed to be easily expanded
     36 \ ** for future STM32 parts with as yet unknown sector sizes.
     37 
     38 internal
     39 
     40 ((
     41 FlashBase constant FlashBase
     42 _FPEC constant _FPEC
     43 
     44 : .FPEC		\ --
     45   _FPEC $24 pdump  ;
     46 ))
     47 
     48 : ?Unlock	\ --
     49 \ +G Unlock the Flash if protected.
     50   _FPEC flCR + @ bit7 and if
     51     _FPEC  flKEY1 over flKEYR + !	\ write keys
     52     flKEY2 over flKEYR + !
     53     bit7 swap flCR + bic!		\ clear LOCK bit
     54   endif
     55 ;
     56 
     57 : waitFlNB	\ -- status
     58 \ +G Wait until the Flash is not busy and return the status.
     59 \ +* The top bit is set if the operation timed out.
     60   $0010:0000 _FPEC begin		\ -- cnt fpec
     61     swap 1- swap  over			\ check timeout counter
     62    while
     63     dup flSR + @ bit0 and 0=
     64   until then
     65   dup flSR + @				\ -- cnt fpec status ; get status
     66   dup $0034 and rot flSR + !		\ -- cnt status ; clear h/w status
     67   swap 0=
     68   if  $8000:0000 or  endif
     69 ;
     70 
     71 : flw!		\ w addr -- status
     72 \ +G Write a 16 bit word to flash. A status value of $20 indicates
     73 \ +* success.
     74   ?Unlock
     75   bit0 _FPEC flCR + or!			\ set PG bit in control reg
     76   w!					\ write data
     77   waitFlNB				\ wait for completion/timeout
     78   bit0 _FPEC flCR + bic!		\ clear PG bit in control reg
     79 ;
     80 
     81 : flPageErase	\ addr -- status
     82 \ +G Erase the page containing addr. A status value of $20
     83 \ +* indicates success.
     84   FlashBase -				\ relative to start of Flash
     85   ?Unlock  _FPEC
     86   bit1 over flCR + or!			\ set PER bit in control reg
     87   swap over flAR + !			\ set address
     88   bit6 swap flCR + or!			\ set STRT bit in control reg
     89   waitFlNB				\ wait for completion/timeout
     90   bit1 _FPEC flCR + bic!		\ clear PER bit in control reg
     91 ;
     92 
     93 : (w!f)		\ w addr --
     94 \ +G 16 bit Flash write primitive.
     95   flw! drop
     96 ;
     97 
     98 : (c!f)		\ b addr --
     99 \ +G 8 bit Flash write primitive.
    100   2dup c@ = if  2drop exit  endif
    101   dup -2 and  swap 1 and if		\ addr bit 0 = 1 (high byte
    102     swap 8 lshift  over c@ or		\ -- addr' b<<8+b0
    103   else
    104     swap  over 1+ c@ 8 lshift or	\ -- addr' b1+b
    105   then
    106   swap (w!f)
    107 ;
    108 
    109 : (!f)		\ x addr --
    110   over $FFFF and over (w!f)
    111   swap 16 rshift swap 2+ (w!f)
    112 ;
    113 
    114 : inFlash?	\ addr -- flag
    115 \ +G Returns true if the address is in the Flash area.
    116   FLASHSTART FLASHEND within		\ Flash range
    117 ;
    118 
    119 external
    120 
    121 : c!f		\ b addr --
    122 \ *G Program any address, including Flash, with an 8 bit value.
    123 \ ** After programming one half of a 16 bit unit with anything
    124 \ ** other than $FF, you cannot program the other half.
    125   dup InFlash?
    126   if  (c!f)  else c!  endif
    127 ;
    128 
    129 : w!f		\ w addr --
    130 \ *G Program any address, including Flash, with a 16 bit value.
    131   dup InFlash?
    132   if  (w!f)  else  w!  endif
    133 ;
    134 
    135 : !f		\ x addr --
    136 \ *G Program any address, including Flash, with a 32 bit value.
    137   dup InFlash?
    138   if  (!f)  else  !  endif
    139 ;
    140 
    141 : FlErase	\ addr len --
    142 \ *G Erase the Flash sectors in the given range.
    143   bounds ?do
    144     i inFlash? if
    145       i flPageErase drop  /FlashPage
    146     else
    147       -1 i !  4
    148     endif
    149   +loop
    150 ;
    151 
    152 internal
    153 : >Flash	\ src dest len --
    154 \ +G Save a memory block to Flash. The address *\i{src}must be
    155 \ +* even. The address *\i{dest} must be the start address of
    156 \ +* empty (erased) Flash. The kernel CRC is added at the end.
    157   2dup FlErase
    158   2dup + crcslot @ swap !f		\ save kernel checksum
    159   bounds ?do
    160     dup @ i !f  cell +
    161   cell +loop
    162   drop
    163 ;
    164 external
    165 
    166 \ ======
    167 \ *> ###
    168 \ ======
    169 
    170 decimal
    171 
    172