\ bootloader410.fs

0 [if]
Copyright (C) 2007 by Charles Shattuck.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

For LGPL information:   http://www.gnu.org/copyleft/lesser.txt

[then]

] \ target compiler

0 org
0 there $ab erase
$03 org $203 again
$0b org $20b again
$13 org $213 again
$1b org $21b again
$23 org $223 again
$2b org $22b again
$33 org $233 again
$3b org $23b again
$43 org $243 again
$4b org $24b again
$53 org $253 again
$5b org $25b again
$63 org $263 again
$6b org $26b again
$73 org $273 again
$7b org $27b again
$83 org $283 again
$8b org \ Code starts here, after interrupts.

\ Entry point of main program when not bootloading.
HERE ( *) $200 org : main ( *) org

:m -led  [ 1 .P2 clr ] m;
:m +led  [ 1 .P2 set ] m;
:m ~led  [ 1 .P2 toggle ] m;

\ debug
0 [if]
:m once 
      7 #for
         0 # 6 #for
            0 # 5 #for  5 #next
         6 #next
      7 #next 
      [ 1 .P2 toggle ] m;
:m blink    begin 3 # once again m;
[then]

\ 
\ 
\ ---------------------[ serial port primitives ]------------------------------
\ 
: 'EMIT begin 1 .SCON until. 1 .SCON clr SBUF (#!) ;
: 'KEY  begin 0 .SCON until. 0 .SCON clr SBUF (#@) ;
: 'KEY-OR-TIMEOUT 
	$f1 # TMOD and! $01 # TMOD ior! \ 16 bit timer 0.
	0 # TH0 #! 0 # TL0 #!   \ Timeout period.
	5 .TCON clr 4 .TCON set \ Start timer0.
	begin
		0 .SCON if.  0 .SCON clr SBUF (#@) ;  then
		5 .TCON if. \ Timer0 overflow.
		   4 .TCON clr \ Stop timer0.
		   main ; \ Run normal program.
		then
	again ;
\  
\ 
\ ---------------------[ flash setup/read/write ]------------------------------
\ 
:m write-setup   \ common code for write & erase
	$80 # IE ior!  \ disable interrupts
	$A5 # FLKEY #! \ write first key byte
	$F1 # FLKEY #! \ write second key byte
m;

:m +write   \ setup for flash write
\	$01 ~# PFE0CN and!  \ clear FLBWE for single byte writes (default)
	write-setup
	$01 # PSCTL #! \ set PSWE, enable write, not erase
m;

:m +erase  \ setup for flash page erase
	write-setup
	$03 # PSCTL #!	\ set PSWE & PSEE, enable write & erase
m;	

:m -write  \ finish flash erase or write
	$00 # PSCTL #!	\ clear PSWE & PSEE to disable erase/write
	$80 ~# IE and!  \ enable interrupts
m;

\ 
\ 
\ ----------------------------[ 410 bootloader ]-------------------------------
: boot 
	$60 ~# PCA0MD and! 	\ Disable watchdog (clear WDTE,WDLCK bits)
	$c7 # OSCICN #!    	\ Internal 24.5MHz oscillator.
   	$a0 # VDM0CN #!        	\ VDD Monitor enabled, VDMLVL=1 (flash write)
   	100 # 3 #for  3 #next  	\ wait ~ 5us for monitor to stabilize
   	$02 # RSTSRC #!  	\ Enable VDD Monitor as a reset source
				\   by setting PORSF bit.
\	$01 ~# PFE0CN and!  	\ clear FLBWE for single byte writes (default)
        $00 # FLSCL #!		\ set FLRT, flash writes <= 25 MHz (default)
\ --- XBR initialization (basic -- before application initialization)
	$01 # XBR0 #!		\ Enable UART only.
	$40 # XBR1 #!     	\ Enable crossbar and weak pullups.	

\ turn test LED on (debug)
\        $02 # P2MDOUT ior!  	\ make P2.1 push-pull
\        +led		    	\ turn on LED

\ Setup serial port.
	$10 # CKCON #!  	\ T1 uses SYSCLK, no prescaler.
	$52 # SCON #!
	$20 # TMOD #!
        $97 # TH1 #!            \ 9600 baud, 24.5MHz
\        $CC # TH1 #!            \ 19.2K baud, sec/bit=52.0833 us/bit
				\ At ~ 24.5 MHz internal, $C8 to $CC work 
				\ with bootloader.  $C9 is ~ 52.4 usec
				\ 'EMIT is ok for $C0 to $CC, but no boot.
				\ For 24.580980 MHz crystal output:
				\ 	$CA -- 51.6 usec/bit, -0.9%
				\ 	$C9 -- 52.8 usec/bit, +1.4%
                                \ For 24.57807 MHz crystal output:
				\ 	$CD -- 51.6 usec/bit, -0.9%
				\       $CC -- 52.4 usec/bit, +0.6% 
				\ 	$CB -- 53.6 usec/bit
				\ 	$CA -- 55.6 usec/bit 
       	6 .TCON set
\	 char R (#) 'EMIT  \ debug - R signals UART setup is ok    
\        begin $1D # delay3 'KEY 'EMIT again  \ debug - echo chars, ck rate
\ --- set external memory address register
        $00 # EMI0CN #! \ 8 bit movx instruction are to page 0
\ --- Only bootload if the amr password comes in time
	'KEY-OR-TIMEOUT
	$a5 # xor if 64 (#) main ; then
	$5a (#) 'EMIT \ signal host we're ready
	'KEY-OR-TIMEOUT
	char a # xor if 64 (#) main ; then
	'KEY-OR-TIMEOUT
	char m # xor if 64 (#) main ; then
	'KEY-OR-TIMEOUT
	char r # xor if 85 (#) main ; then
\ --- setup the download
	$a5 (#) 'EMIT 	 \ signal readiness to host
	'KEY 		 \ get number of pages
	0=if main ; then \ Don't download 0 pages.
	64 # =if then 0=if' 62 (#) then \ clip
	7 (#!)    	 \ save number of pages in R7
\ --- erase pages
	$200 ##p! 	 \ first page to erase
	begin 		 \ erase loop
		+erase (!x) -write
		DPH inc DPH inc ( next 512 byte page) 
	7 #next
	7 (#!) 		\ restore page counter
	$5a (#) 'EMIT 	\ signal readiness to host
\ --- accept bytes and write them into flash, starting at $200
	$200 ##p!
	begin
		0 # 6 #for
			'KEY +write (!x) -write p+
			'KEY +write (!x) -write p+
		6 #next
		7 (#@) 'EMIT
	7 #next
	'KEY \ Wait for signal to start running program.
	( -led)  main ;
HERE ( *) 0 org : reset boot ; ( *) org

\ 
\  
0 [if] ------------------------[ revisions ]-----------------------------------

Date	By   SVN Comment
=======	=== ==== ==============================================================


------------------------------------------------------------------------ [then]

