
compiletoflash

\ 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/ 2 f.n ." 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- 2 f.n ." °C "                 \ Calculate uncalibrated temperature...
;

: delayticks ( ticks -- ) \ Wait desired number of ticks with 32768 Hz.

\ First Timer, used for PWM
\  ['] wakeup irq-timera0 ! \ Set Wakeup-Interrupt
\  $10  $0162 ! \ Enable Interrupt
\  0    $0170 ! \ Clear TAR
\       $0172 ! \ Set desired delay
\  $110 $0160 ! \ ACLK, Up-Mode
\  lpm3         \ Sleep in LPM3
\  0 $160 !     \ Stop Timer

\ Second Timer, free
  ['] wakeup irq-timerb0 ! \ Set Wakeup-Interrupt
  $10  $0182 ! \ Enable Interrupt
  0    $0190 ! \ Clear TAR
       $0192 ! \ Set desired delay
  $110 $0180 ! \ ACLK, Up-Mode
  lpm3         \ Sleep in LPM3
  0 $180 !     \ Stop Timer
;

: ms ( ms -- ) 32768 1000 u*/ delayticks ; \ Maximum is about 1000 ms

: 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
  $110 $160 ! \ ACLK/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 eraseflashfrom
;

: u.4 ( u -- ) 0 <# # # # # #> type ;
: u.2 ( u -- ) 0 <# # # #> type ;

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

  \ MSP430F2274: Complete: $FFFF $8000
  \ MSP430G2553: Complete: $FFFF $C000

  $FFFF $C000 \ Complete image with Dictionary
  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 u.4 ." 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@ u.2 \ Print data with 2 digits
        i c@ +     \ Sum it up for Checksum
      loop

      negate u.2  \ 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
