
\ -----------------------------------------------------------------------------
\  Warteroutinen für 168 MHz
\ -----------------------------------------------------------------------------

: us ( u -- )    168 * delay-cycles ;
: ms ( u -- ) 168000 * delay-cycles ;

\ -----------------------------------------------------------------------------
\  Verbindung mit dem STM32F407 Discovery
\ -----------------------------------------------------------------------------

\ SBW-JTAG für einen MSP430F2012
\ Läuft auf einem STM32F407 mit Mecrisp-Stellaris RA

\ Verbinden mit vier Drähten:

\ 3.3V an Vcc
\ PC8 an /Reset
\ PC9 an Test
\ GND an GND

GPIOC_IDR   constant JTAGIN
GPIOC_ODR   constant JTAGOUT
GPIOC_MODER constant JTAGDIR

8 constant SBWDAT-Pin
9 constant SBWCLK-Pin

 1 variable   jtaglength \ A tiny delay for JTAG timing which isn't critical

44 variable strobelength \ --> tclk-strobes gives 409 kHz
                         \ Frequency fFTG = 257..476kHz (t = 3.9..2.1us).

:   jtagdelay ( -- )   jtaglength @ begin 1- dup 0= until drop ;
: strobedelay ( -- ) strobelength @ begin 1- dup 0= until drop ;

%01 SBWDAT-Pin 2* lshift constant SBWDATODIR
%01 SBWCLK-Pin 2* lshift constant SBWCLKDIR

1 SBWDAT-Pin lshift constant SBWDATO \ JTAG data_out pin in SBW mode
1 SBWDAT-Pin lshift constant SBWDATI \ JTAG data in pin in SBW mode
1 SBWCLK-Pin lshift constant SBWCLK  \ JTAG clock pin in SBW mode

\ -----------------------------------------------------------------------------
: InitTarget ( -- )
             SBWDATO      JTAGOUT bis!
             SBWCLK       JTAGOUT bic!
  SBWDATODIR SBWCLKDIR or JTAGDIR bis!  \ Outputs
  jtagdelay
;

\ -----------------------------------------------------------------------------
: ReleaseTarget ( -- )
  SBWDATODIR SBWCLKDIR or JTAGDIR bic!  \ High-Z state
;
