PICNOS= 0 1 3
PROGRAMS= led-flash send-serial panic reply-serial \
- nmra-stream nmra-stream,slow tblrd_test \
- i2c-test i2c-test,slow
+ nmra-stream nmra-stream,slow
+
+# These programs haven't been updated to cope with
+# master and slave pics having different clock speeds:
+# i2c-test i2c-test,slow tblrd_test
ROUTINES= routines-led
LIBS= routines.lib
; boilerplate:
include /usr/share/gputils/header/p18f458.inc
- include ../iwjpictest/clockvaries.inc
radix dec
+ include ../iwjpictest/clockvaries.inc
extern led_green
extern led_red
; pin 21 (per-pic-led, RD2/PSP2/C1IN) states: high H = green, low L = red, float Z = black
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
code
; pin initial config
bcf TRISE,4,0 ; turn off PSPMODE (Data Sheet p100/101)
; timer initial config
- bcf T0CON,6,0 ; p107 Timer0 -> 16bit mode
- bcf T0CON,5,0 ; timer0 use internal clock
- bsc_morse_t0con_012 ; use prescaler? and configure it
+ morse_t0setup mclock, (1<<TMR0ON), -1, -1
; actually do stuff
call green
call waiting
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
; 10 10 10 00 | 1110 1110 | 1110 00 10 | 10 10
include common.inc
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
extern led_green
; re-initialise timer0 config
- bcf T0CON,6,0 ; p107 Timer0 -> 16bit mode
- bcf T0CON,5,0 ; timer0 use internal clock
- bsc_morse_t0con_012 ; use prescaler? and configure it
+ morse_t0setup mclock, (1<<TMR0ON), -1, -1
clrf BLANK,0
panic_loop
; pin 21 (per-pic-led, RD2/PSP2/C1IN) states: high H = green, low L = red, float Z = black
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
extern led_green
; timer0 initial config
- bcf T0CON,6,0 ; p107 Timer0 -> 16bit mode
- bcf T0CON,5,0 ; timer0 use internal clock
- bcf INTCON,5,0 ; clear TMR0IE => mask interrupt
- bsc_morse_t0con_012 ; use prescaler? and configure it
+ morse_t0setup mclock, (1<<TMR0ON), -1, -1
main
call led_green
; routines-led
; subroutines for controlling the per-pic LED (pin 21, DS100)
+clock equ -1
include common.inc
global led_green
; pin 21 (per-pic-led, RD2/PSP2/C1IN) states: high H = green, low L = red, float Z = black
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
code
PICNOS= 0 1 2
PROGRAMS= test-sofar
-OBJS_test-sofar= vectors.o panic.o routines-led.o i2clib.o
+OBJS_test-sofar= vectors.o panic.o routines-led.o i2clib.o misc.o
XCODEN_test-sofar= morse
XCODE1_test-sofar= blank2 blank6
INCLUDES= common.inc \
final.inc \
i2clib.inc \
+ misc.fin \
panic.fin \
panic.inc \
routines-led.fin \
include panic.inc
include morse+auto.inc
include ../iwjpictest/insn-aliases.inc
+clock equ -1
include ../iwjpictest/clockvaries.inc
;****************************************************************************
include i2clib.inc
include panic.fin
include routines-led.fin
+ include misc.fin
end
include /usr/share/gputils/header/p18f458.inc
radix dec
include ../iwjpictest/insn-aliases.inc
+
+clock equ 0
include ../iwjpictest/clockvaries.inc
include panic.inc
include morse+auto.inc
--- /dev/null
+;======================================================================
+; GENERALLY USEFUL ROUTINES
+
+ include /usr/share/gputils/header/p18f458.inc
+ include ../iwjpictest/insn-aliases.inc
+ radix dec
+ code
+
+read_pic_no
+; W undefined pic number from ID loc 0
+; status Z undefined 1 iff master PIC
+ ; read pic no from ID locations
+ mov_lw 0x20
+ mov_wf TBLPTRU
+ clr_f TBLPTRH
+ clr_f TBLPTRL
+
+ tblrd *+
+ dw 0xffff ; silicon errata: B4 issue 4
+ mov_fw TABLAT
+
+ return
+
+ include misc.fin
+ end
--- /dev/null
+ extern read_pic_no
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include ../iwjpictest/clockvaries.inc
panic_morse res 1 ; # bytes of morse msg in panic readout
panic_regs res 1 ; # registers in panic readout
+t0l_count res 1
+t0h_count res 1
;****************************************************************************
clr_f STKPTR ; avoids stack overruns
; re-initialise timer0 config
- mov_lw (1<<TMR0ON) | morse_t0scale ; Enable, 16-bit, timer, prescaler
- mov_wf T0CON
+ call read_pic_no
+ bra_z panic_setup_if_master
+panic_setup_if_slave
+ morse_t0setup sclock, (1<<TMR0ON), t0l_count, t0h_count
+ bra panic_setup_endif_masterslave
+panic_setup_if_master
+ morse_t0setup mclock, (1<<TMR0ON), t0l_count, t0h_count
+panic_setup_endif_masterslave
; get # bytes of morse msg, # registers in panic readout, message start addr.
; back from condensed message start addr. stored in panic_address
bc_f INTCON,2 ; clear timer0 interrupt bit (p109)
; Interrupt happens on overflow. So start at 65535-morse_t0cycles:
- mov_lw (65535-morse_t0cycles) / 256
+ mov_fw t0h_count
mov_wf TMR0H ; p107 set high byte of timer0 (buffered,
; only actually set when write to tmr0l occurs)
- mov_lw (65535-morse_t0cycles) & 0xff
+ mov_fw t0l_count
mov_wf TMR0L ; set timer0 low byte - timer now set
waiting_loop
bt_f_if0 INTCON,TMR0IF
clr_f INTCON
bs_f RCON, IPEN ; interrupt priorities
- ; read pic no from ID locations
- mov_lw 0x20
- mov_wf TBLPTRU
- clr_f TBLPTRH
- clr_f TBLPTRL
-
- tblrd *
- dw 0xffff ; silicon errata: B4 issue 4
-
- mov_fw TABLAT
+ call read_pic_no
mov_wf picno
bra_z master
goto slave
mov_wf INTCON
m_infinite
- call delay
+ call m_delay
call led_black
- call delay
- call delay
- call delay
- call delay
- call delay
- call delay
- call delay
+ call m_delay4
+ call m_delay2
+ call m_delay
call led_green
bra m_infinite
mov_wf t
s_shownum_loop
- call delay
+ call s_delay
call led_red
- call delay
+ call s_delay
call led_black
dec_f_ifnz t
;======================================================================
- define_busywait_delay ; from iwjpictest/clockvaries.inc
+m_delay4 rcall m_delay2
+m_delay2 rcall m_delay
+m_delay
+ implement_busywait_delay mclock
+ return
+
+s_delay
+ implement_busywait_delay sclock
+ return
;======================================================================
; SERIAL LIBRARY
; ... uh, this isn't going to work at 1MHz because that's only 12.5
; insns per NMRA division
+
;----------------------------------------------------------------------
; for morse at 18wpm according to the PARIS standard
; (ie a unit time of 66ms) using timer0 (DS p108)
- if mclock==20000 ; Fosc=20MHz, Fcy=5MHz ie 200ns
-morse_t0scale equ 0011b ; 1:8 => 1.6us
-morse_t0cycles equ 41250 ; * 1.6us = 66ms
+morse_t0setup macro clock, t0con_other, lcount, hcount
+ local t0scale
+ local t0cycles
+ if clock==20000 ; Fosc=20MHz, Fcy=5MHz ie 200ns
+t0scale equ 0011b ; 1:8 => 1.6us
+t0cycles equ 41250 ; * 1.6us = 66ms
endif
- if mclock==1000 ; Fosc=1MHz ie 1us, Fcy=4us
-morse_t0scale equ 1000b ; do not use prescaler
-morse_t0cycles equ 16500 ; * 4us = 66ms
+ if clock==2000 ; Fosc=2MHz ie 500ns, Fcy=2us
+t0scale equ 1000b ; do not use prescaler
+t0cycles equ 33000 ; * 2us = 66ms
+ endif
+ if clock==1000 ; Fosc=1MHz ie 1us, Fcy=4us
+t0scale equ 1000b ; do not use prescaler
+t0cycles equ 16500 ; * 4us = 66ms
+ endif
+ movlw t0con_other | t0scale
+ movwf T0CON
+ if hcount>=0
+ movlw (65535-t0cycles) / 256
+ movwf hcount
+ endif
+ if lcount>=0
+ movlw (65535-t0cycles) & 0xff
+ movwf hcount
endif
-
-bsc_morse_t0con_012 macro
- movf T0CON,0,0
- andlw 0xf0
- iorlw morse_t0scale
- movwf T0CON,0
endm
;----------------------------------------------------------------------
; busy-wait delay loop, originally from flasher.asm
- if mclock==20000 ; each cycle 0.2us
+ if clock==20000 ; each cycle 0.2us
delay_fastloop equ 1 ; each medium loop = ~40ms
delay_slowexp equ 2 ; 2^2 * 40ms = 160ms
endif
- if mclock==1000 ; each cycle 4us
+ if clock==1000 ; each cycle 4us
delay_fastloop equ 0 ; each medium loop = ~3ms
delay_slowexp equ 6 ; 2^6 * 3ms = 192ms
endif
-define_busywait_delay macro
-delay
+implement_busywait_delay macro tclock
; we always leave each loop when its counter has reached 0, so on
; entry we assume it's already 0. For the loops which want to
; set a bit which says how fast the led
; should flash and count down from 2^(that bit)
- bsf delay_countslow, delay_slowexp, 0
+ if tclock >= 3915
+ local fast=1
+ local medium_cycles=198000
+ else
+ local fast=0
+ local medium_cycles=768
+ endif
+ ; now 1/(medium_period in ms)
+ ; = tclock / (medium_cycles*4)
+ movlw 50 * tclock / medium_cycles ; 200ms
+ movwf delay_countslow, 0
+
+ local delayslow_loop
+ local delaymedium_loop
delayslow_loop
delaymedium_loop
- if delay_fastloop
+ if fast
+ local delayfast_loop
delayfast_loop
decfsz delay_countfast, 1, 0 ; 1 cycle
goto delayfast_loop ; 2 cycles (skipped or not)
; exited delayfast_loop ; total: 3 * 256 = 768 cycles
endif
-
+
decfsz delay_countmedium, 1, 0 ; 1 cycle
goto delaymedium_loop ; 2 cycles (skipped or not)
goto delayslow_loop
; exited delayslow_loop
+ endm
+
+define_busywait_delay macro
+delay
+ implement_busywait_delay clock
return
endm
delay_countmedium equ 0x01
delay_countslow equ 0x02
+clock equ mclock
include clockvaries.inc
code
;
; `printset' means print if `printset' flag is set, otherwise just evaluate
- nolist
include /usr/share/gputils/header/p18f458.inc
+ radix dec
+
+clock equ mclock
include insn-aliases.inc
include clockvaries.inc
; list
- radix dec
;------------------------------------------------------------
; variable declarations and RAM memory map
# FOO-entire0.hex FOO.o idlocs0.o config.o
# perpicNUMBER.hex idlocsNUMBER.o config.o
-ASFLAGS= -Dmclock=20000 -Dsclock=20000
-#ASFLAGS= -Dmclock=1000 -Dsclock=1000
+#ASFLAGS= -Dmclock=20000 -Dsclock=20000
+ASFLAGS= -Dmclock=20000 -Dsclock=2000
LINK= gplink -m -o $@ $^
ASSEMBLE= gpasm -p 18f458 $(ASFLAGS)