(* Next step for the 12 bytes field idea for noForth  \ zie regel 77 (an)

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

Parameter order for cold:

Word: Flash      00 - Flash programming frequency word
Byte: dco-lb     02 - low byte address
Byte: br0        03 - low byte baudrate
Byte: br1        04 - high byte baudrate
Byte: mctl       05 - tuning baudrate
Word: Pin-addr   06 - address of input port
Byte: sw-bit     08 - bit number for switch
Byte: leds       09 - bit mask for leds
Word: Pout-dir   0A - Address of output direction register

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

Usage example 1 MHz DCO for LP:
ACTUAL              \ Get current parameters
A542 10FE DCO       \ DCO at 1 MHz
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
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
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
YES!            Copies TEMP parameter table to Flash
DCO             ( dco flash --)    Change DCO parameters & print
BAUDRATE        ( br2 br1 br0 -- ) Change RS232 parameters & print
SWITCH          ( mask addr -- )   Change SWITCH parameters & print
LEDS            ( mask addr -- )   Change LEDS parameters & print

Example:



*)

hex
frozen 40 + dm 12 -         \ 10B4 = start of storage area for Launchpad
dup constant IO

A550 over rom!   2 +          \ Flash programming code for 8MHz
  FC over romc!  1+    20 ms  \ Low byte of 8 MHz DCO settings address
                              \ Add 1000 to it and we get the real address
                              \ For 16 MHz this low byte is: F8
  41 over romc!  1+    20 ms  \ Baudrate settings
  03 over romc!  1+    20 ms
  04 over romc!  1+    20 ms
\ Even address
  20 over rom!   2 +   20 ms  \ S? settings
  08 over romc!  1+    20 ms
\ Uneven address
  41 over romc!  1+    20 ms  \ Led settings
\ Even address
  22 swap rom!         20 ms  \ Led output settings

\ Kan bovenstaande initiëring niet met DCO BAUDRATE SWITCH LEDS YES! ?  <------ Ja natuurlijk ga je gang Albert ------>

code COLD-LP    ( -- )      \ Activate Launchpad
\ stop watchdog & set oscillator freq.
    5A80 # 0120 & mov       \ WDTCTL   Stop watchdog


\ 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

\ 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

\ Activate LED(s)
    IO 9 + # tos mov        \ Start at led bitmask
    tos )+ w .b mov         \ Fetch bit number(s)
    tos )+ day mov          \ Fetch PxDIR address
    #-1 day add             \ To 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 4 + # tos mov
    tos )+ day mov
    tos )+ tos .b mov
    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 .b mov         \ Get low byte of DCO data
    1000 # w bis            \ Add high address part
    w )+ 0056 & .b mov      \ DCOCTL  Set DCO = xx MHz
    w ) 0057 & .b mov       \ BCSCTL1
\ 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 S?
    tos )+ day mov          \ Fetch port address
    tos )+ sun .b mov       \ Fetch bit number
    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
\ Activate LED(s)
    tos )+ w .b mov         \ Fetch bit number(s)
    tos ) day mov           \ Fetch PxDIR address
    w day ) .b bis          \ Set dir for leds
    sp )+ tos mov           \ Remove address
    next
end-code

create TEMP  0C allot
: .CONF         \ Print configuration data for MSP430!
    cr temp @+ u. count 1000 or u. ."   DCO   \ dco flash"
    cr count . count . count . ."   BAUDRATE    \ br2 br1 br0"
    cr @+ u. count . ."   SWITCH   \ mask addr"
    cr count  swap @ u.  .  ."   LEDS   \ mask addr"
    cr ;


: DCO       ( dco flash -- )    swap temp !  temp 2 + c! ;
: BAUDRATE  ( br2 br1 br0 -- )  3 5 do  temp i + c!  -1 +loop ;
: SWITCH    ( mask addr -- )    temp 6 + !  temp 8 + c! ;
: LEDS      ( mask addr -- )    swap temp 9 + c!  temp 0A + ! ;
: START     ( -- )              frozen [ 40 0C - ] literal +  config ;
: TEST      ( -- )              temp config ;

: ACTUAL    ( -- )
    frozen [ 40 0C - ] literal +  temp 0C move  .conf ;

: YES!      ( -- )
    frozen  here  [ 40 0C - ] literal  tidy)
    temp  frozen [ 40 0C - ] literal +  0C rommove ;

' start  to app
shield START\  freeze

\ End
