(* Next step for the 14 bytes field idea for noForth

*** Code kan geladen op LP en Egel kit en werkt ***

*WO* Rare kronkels verwijderd en UART vlag toegevoegd
     Extra vlag voor UART selectie toegevoegd, 0 of -1

Parameter order for cold:

Word: Flash      00 - Flash programming frequency word
Word: dco        02 - Full address
Byte: usci-flag  04 - Choose communication port (if available)
Byte: sw-bit     05 - Bit number for switch
Word: Pin-addr   06 - Address of input port
Byte: br0        08 - Low byte baudrate
Byte: br1        09 - High byte baudrate
Byte: mctl       0A - Tuning baudrate
Byte: leds       0B - Bit mask for leds
Word: Pout-addr  0C - Address of output port

--- Erase parameter block from info-B
    frozen  here  32  tidy)
---

Usage example 1 MHz DCO for LP:
ACTUAL              \ Get current parameters
A542 10FE DCO       \ DCO at 1 MHz
00 68 00 02 BAUDRATE \ Keep at 9600 baud
TEST                \ Try out
YES!                \ Freeze new setup

Usage example 16 MHz DCO for LP:
ACTUAL              \ Get current parameters
A562 10F8 DCO       \ DCO at 16 MHz
00 82 06 0C BAUDRATE \ Keep at 9600 baud
TEST                \ Try out
YES!                \ Freeze new setup

Usage example 1 MHz DCO for Egel kit:
ACTUAL              \ Get current parameters
A542 10FE DCO       \ DCO at 1 MHz
00 1A 00 00 BAUDRATE \ Keep at 38K4 baud
TEST                \ Try out
YES!                \ Freeze new setup

---

ACTUAL          Copies parameters to RAM & shows them
TEMP CONFIG     Configure MSP430 with temporary parameters
.CONF
YES!            Copies TEMP parameter table to Flash
DCO             ( dco flash --)    Change DCO parameters & print
BAUDRATE        ( flag br2 br1 br0 -- ) Change RS232 parameters & print
SWITCH          ( mask addr -- )   Change SWITCH parameters & print
LEDS            ( mask addr -- )   Change LEDS parameters & print

---

\ Even address
A550 over rom!   2 +          \ Flash programming code for 8MHz
10FC over rom!   2 +   20 ms  \ Address of 8 MHz DCO parameters
                              \ For 16 MHz this is: 10F8
  00 over romc!  1+    20 ms  \ UART select flag
\ Uneven address
  08 over romc!  1+    20 ms  \ S? bitmask
\ Even address
  20 over rom!   2 +   20 ms  \ S? PxIN address
  41 over romc!  1+    20 ms  \ Baudrate settings
  03 over romc!  1+    20 ms
  04 over romc!  1+    20 ms
\ Uneven address
  41 over romc!  1+    20 ms  \ Led bitmask
\ Even address
  21 swap rom!         20 ms  \ Led I/O PxOUT address

*)

hex
40 constant #INFO       \ INFO block size needed in target only
                        \ The size is 80 for most other MSP430 MPU's

frozen #info + dm 14 -  \ 10B4 = start of storage area for Launchpad
dup constant IO

code INIT-IO    ( -- )      \ Activate Launchpad
\ stop watchdog & set oscillator freq.
    5A80 # 0120 & mov       \ WDTCTL   Stop watchdog
\ Uart setup starts with i/o pin direction
    6 # sun mov
    sun 0026 & .b mov       \ P1SEL    Uart init. to p1.1 & p1.2
    sun 0041 & .b mov       \ P1SEL2
    #0 0000 & .b mov        \ IE1      Erase interrupt flags
\ Default setup for port-1
    #-1 22 & .b bis         \ P1DIR    P1 all bits outputs
    #-1 21 & .b bic         \ P1OUT    P1 all bits low
\ Default setup for port-2
    #-1 2A & .b bis         \ P2DIR    P2 all bits outputs
    #-1 29 & .b bic         \ P2OUT    P2 all bits low
\ Activate LED(s)
    IO B + # tos mov        \ Start at led bitmask
    tos )+ w .b mov         \ Fetch bit mask
    tos )+ day mov          \ Fetch PxOUT address
    w day ) .b bis          \ PxOUT    Led(s) on
\ Wait, to hold leds on for a while
    #0 sun mov
    begin,  #1 sun sub  0=? until,
    w day ) .b bic          \ PxOUT    Led(s) off
    next
end-code

code S?     ( -- result )   \ For all versions
    tos sp -) mov
    IO 5 + # sun mov
    sun )+ tos .b mov       \ Bit mask
    sun )+ day mov          \ PxIN address
    day ) tos .b bia
    next
end-code

code CONFIG ( adr -- )
\ Flash initialisation for all valid DCO frequencies 257kHz to 476kHz
    tos )+ 012A & mov       \ FCTL2    Mclk/1+n=???kHz for flash timing
    tos )+ w mov            \ Get address DCO data
    w )+ 0056 & .b mov      \ DCOCTL  Set DCO = xx MHz
    w ) 0057 & .b mov       \ BCSCTL1
\ Activate S?
    #1 tos add              \ Skip UART flag
    tos )+ sun .b mov       \ Fetch bit number
    tos )+ day mov          \ Fetch port address
    sun day ) .b bic        \ Set dir for switch
    #1 day add              \ To PxOUT address
    sun  day ) bis          \ PxOUT    Set Px.y is pullup
    6 # day add             \ To PxREN address
    sun day ) .b bis        \ PxREN    Px activate resistor
\ Uart data from Flash
    80 # 0061 & .b bis      \ UCA0CTL1 Uart use smclk
    tos )+ 0062 & .b mov    \ UCA0BR0  baudrate xx baud
    tos )+ 0063 & .b mov    \ UCA0BR1
    tos )+ 0064 & .b mov    \ UCA0MCTL Modulation ucbrsx = x
    #1 0061 & .b bic        \ UCA0CTL1 Initialize usci state machine
\ Activate LED(s)
    tos )+ w .b mov         \ Fetch bit number(s)
    tos ) day mov           \ Fetch PxOUT address
    #1 day add              \ To PxDIR address
    w day ) .b bis          \ Set dir for leds
    sp )+ tos mov           \ Remove address
    next
end-code

create TEMP  0E allot
: .CONFIG       \ Print configuration data for MSP430!
    cr temp @+ u.  @+ u. ."  DCO   \ dco flash"
    count >r  ( Save UART flag )
    cr count . @+ u.  ."  SWITCH   \ mask addr"
    cr r> 0<> . count . count . count . ."  BAUDRATE    \ flag br2 br1 br0"
    cr count . @ u.  ."  LEDS   \ mask addr"
    cr ;

: BAUDRATE  ( fl br2 br1 br0 -- )
    temp A + c!  temp 9 + c! temp 8 + c!  0<> temp 4 + c! ;

: DCO       ( dco flash -- )    temp 2 + !  temp ! ;
: SWITCH    ( mask addr -- )    temp 6 + !  temp 5 + c! ;
: LEDS      ( mask addr -- )    temp 0C + !  temp B + c! ;
: START     ( -- )              frozen [ #info 0E - ] literal +  config ;
: TEST      ( -- )              temp config ;

: ACTUAL    ( -- )
    frozen [ #info 0E - ] literal +  temp 0E move  .config ;

: YES!      ( -- )
    frozen  here  [ #info 0E - ] literal  tidy)
    temp  frozen [ #info 0E - ] literal +  0E rommove ;

A550 10FC DCO           \ Initialise settings
08 20 SWITCH
0 41 3 4 BAUDRATE
41 21 LEDS
YES!

' start  to app
shield START\  freeze

\ End
