compiletoflash

: 0-foldable $40 setflags immediate ; 
: 1-foldable $41 setflags immediate ; 
: 2-foldable $42 setflags immediate ;

: ( [char] ) parse drop immediate 0-foldable ;  (  Forth-Style-Comment )
: { [char] } parse drop immediate 0-foldable ;  { Pascal-Style-Comment }
: \        0 parse drop immediate 0-foldable ;  \ Comment till end of line 

32 constant bl
: cr    10 emit ;
: space bl emit ;
: spaces 0 ?do space loop ;

\ Ports

$20 constant P1IN
$21 constant P1OUT
$22 constant P1DIR
$23 constant P1IFG
$24 constant P1IES
$25 constant P1IE
$26 constant P1SEL
$27 constant P1REN
$41 constant P1SEL2

$28 constant P2IN
$29 constant P2OUT
$2A constant P2DIR
$2B constant P2IFG
$2C constant P2IES
$2D constant P2IE
$2E constant P2SEL
$2F constant P2REN
$42 constant P2SEL2

$4A constant ADC10AE0 \ If you want to use Pins of Port 1 as analog inputs, you have to set the corresponding bits in ADC10AE0 !

: init \ Launchpad hardware initialisations
  8 p1out cbis! \ High
  8 p1ren cbis! \ Pullup for button

  1 64 or p1out cbic! \ LEDs off
  1 64 or p1dir cbis! \ LEDs are outputs
;

\ Measure Vcc/2 on analog channel 11 with 2.5V reference.
: vcc. 0  11 analog  204,6 f/ f. ." V " ; 

\ Measure temperature on analog channel 10.
: temp.
  0
  0.  200 0 do 10 adc-1.5 0 d+ loop 200 um/mod nip  \ Average 200 measurements with 1.5V reference because of strong noise.
  0,41263 f* 280,28 d- f. ." °C "                    \ Calculate uncalibrated temperature...
;

\ Busy waits for the given time or slightly more, if interrupts are active.
\ DCO clock is only accurate to +-3% and varies with Vcc and temperature.
\ For precise timings, connect a crystal and use timer.
\ 8 cycles per loop run for 1 us @ 8 MHz.

: us 0 ?do [ $3C00 , $3C00 , ] loop ;
: ms 0 ?do 998 us loop ;


: pwm-init ( Interval -- ) \ Init PWM on green LED
  64 p1dir cbis! \ Output
  64 p1sel cbis! \ Timer special function

  \ Configure Timer-A

       $172 ! \ PWM Interval in cycles
  $E0  $164 ! \ CCR1 Set/Reset
  0    $174 ! \ CCR1 PWM Duty Cycle
  $210 $160 ! \ SMCLK/1, upmode
;

: pwm ( Duty-Cycle -- ) $174 ! ; \ Sets duty cycle


: random ( -- u ) 
 ( Generiert Zufallszahlen mit dem Rauschen vom Temperatursensor am ADC )
 ( Random numbers with noise of temperature sensor on ADC )
   0
   16 0 do
     shl
    10 analog 1 and
    xor
  loop
;


: cornerstone ( Name ) ( -- )
  <builds begin here $1FF and while 0 , repeat
  does>   begin dup  $1FF and while 1+  repeat

  [
    $c232 ,                 \ dint
    $43c2 , $0000 ,         \ clr.b &IE1
    $40b2 , $5a80 , $0120 , \ mov #5A80h, &WDTCTL
    $443a ,                 \ mov @r4+, r10

    $4030 , ' eraseflash 16 + ,  \ br #eraseflash-interna
  ]
;


: hex.  0 <# # # # # #> type ;
: chex. 0 <# # # #> type ;

: hexdump ( -- ) \ Dumps complete Flash
  cr hex

  \ MSP430F2274: Complete: $FFFF $8000 Dictionary only: $DBFF $8000
  \ MSP430G2553: Complete: $FFFF $C000 Dictionary only: $DBFF $C000

  $DBFF $C000 \ Dictionary only for faster dump
  do
    \ Check if it would be $FFFF only:
    0                 \ Not worthy to print
    i #16 + i do      \ Scan data
      i c@ $FF <> or  \ Set flag if there is a non-$FF byte
    loop

    if
      ." :10" i hex. ." 00" \ Write record-intro with 4 digits.
      $10                   \ Begin checksum
      i    $FF and +        \ Sum the address bytes 
      i >< $FF and +        \ separately into the checksum

      i #16 + i do
        i c@ chex. \ Print data with 2 digits
        i c@ +     \ Sum it up for Checksum
      loop

      negate chex.  \ Write Checksum
      cr
    then

  #16 +loop
\  ." :00000001FF" cr
  decimal
; 

cornerstone Rewind-to-Basis

\ Unlike ANS-Marker, the defined "Rewind-to-Basis" stays in Flash and doesn't remove itself upon invocation. 
\ I think, this should be more useful for its given task to conserve some basics.

compiletoram
hexdump
