+++ /dev/null
-;======================================================================
-;
-; test-sofar test harness which tests functionality written `so far'
-; (as new functionality is available this will be extended)
-;
-;----------------------------------------------------------------------
-;
-; Current behaviour:
-;
-; Starts up, reads PIC number from flash
-; Flashes Red N times for PIC N
-; Sets LED to black on slave, blue on master
-; prints | on master
-;
-; Then, on master PIC
-; initialises i2c controller with i2clib
-; sets buffer to empty, first byte is 0xff
-; and then:
-; on serial interrupt
-; call some i2cm_... function
-; on i2c interrupt
-; write out some character
-; in main loop
-; flashes blue flash every once in a while
-;
-; Input characters:
-; 1-9 if we are to write (readwrite -ve):
-; i2cm_write_start; i2cmu_write_next_byte will be handled
-; automatically: we transmit every byte before the
-; buffer pointer.
-; if we are to read (readwrite 0 or +ve):
-; i2cm_read_start (buffer empty)
-;
-; 0x00 switch to crash readout mode (see README.protocol)
-;
-; 0x80-ff make buffer consisting of only that byte
-; (ie, clear top bit, store at start of buffer,
-; set buffer pointer to next byte, and select writing)
-;
-; 0x01-0f select reading, automatic advance: store this in readwrite
-;
-; 0x10-1f panic, showing character code
-;
-; ^ set top bit of first byte of buffer
-;
-; , i2cm_read_another } if readwrite is zero
-; . i2cm_read_done } then these are not called;
-; } instead, we count down automatically
-;
-; SPC set buffer pointer to start, select reading (manual)
-; 0 set buffer pointer to start, select writing
-; other append char to buffer, select writing
-;
-; + turn on booster (polarity=L) and cdu
-; $ toggle hex mode for reading
-;
-; Output characters:
-; SPC i2cmu_done (writing or manual reading)
-; other i2cmu_read_got_byte
-;
-;======================================================================
-
- include common.inc
-
- udata_acs
-
-ch res 1
-mode res 1
-mode_readhex equ 0
-mode_crashread res 1
-
-readwrite res 1
- ; ff means we're writing or going to write, FSR1
- ; 00 means we're to read interactively
- ; 01-0f is counter of bytes to read
-
-delay_countfast res 1
-delay_countmedium res 1
-delay_countslow res 1
-
- code
-
-;----------
-master_interrupt_high
- panic morse_IP
-
-;----------
-master_interrupt_low
- enter_interrupt_low
- call i2cm_intrl
- call serial_intrl
- panic morse_IL
-
-;----------------------------------------------------------------------
-; MASTER
-;
-; buffer is 0x200 onwards
-; FSR0 is buffer pointer
-; FSR2 is the write cursor
-; FSR1 is the crash readout protocol cursor
-; (and may be trashed by non-crash-readout things)
-
-master
- clr_f mode
-
- call bitnum2bit_init
- call points_local_init
- call polarity_local_init
- call detect_local_init
-
- call serial_setup
- call i2cm_init
- call led_green
-
- mov_lfsr 0x0200, 1
- rcall m_buffer_reset
-
- clr_f readwrite
-
- mov_lw '|'
- rcall serial_write_char
-
- bs_f INTCON, GIEH
- bs_f INTCON, GIEL
-
-m_infinite
- call m_delay
- call led_black
- call m_delay4
- call m_delay2
- call m_delay
- call led_green
- bra m_infinite
-
-;----------
-serialu_read_char
-; W character read any
- mov_wf ch
-
- bt_f_if1 ch, 7
- bra m_ch_setonemsg
-
- mov_lw ~0x0f
- and_wfw ch
- bra_z m_ch_setreadwrite
-
- mov_lw ~0x1f
- and_wfw ch
- bra_z m_ch_readown
-
- ; digit ?
- mov_fw ch
- add_lw -('9'+1)
- bra_nn m_ch_notdigit
- add_lw ('9'+1) - '0'
- bra_z m_ch_0
- bra_nn m_ch_digit
-
-m_ch_notdigit
-
- bt_f_if1 mode, mode_crashread
- bra m_ch_crashsetown
-
- mov_lw ','
- xor_wfw ch
- bra_z m_ch_comma
-
- mov_lw '^'
- xor_wfw ch
- bra_z m_ch_hat
-
- mov_lw '.'
- xor_wfw ch
- bra_z m_ch_stop
-
- mov_lw ' '
- xor_wfw ch
- bra_z m_ch_spc
-
- mov_lw '+'
- xor_wfw ch
- bra_z m_ch_plus
-
- mov_lw '$'
- xor_wfw ch
- bra_z m_ch_dollar
-
- mov_fw ch
- mov_wf POSTINC0
- clr_f INDF0
- clr_f FSR0H ; prevent crashes due to wrapping
- bra m_buffer_fix_fsr0h
-
-;----------
-m_ch_setonemsg
- mov_lfsr 0x200,0
- mov_fw ch
- and_lw 0x7f
- mov_wf POSTINC0
- set_f readwrite
- return
-
-;----------
-m_ch_bad
- panic morse_X
-
-;----------
-m_ch_hat
- mov_lfsr 0x200,1
- bs_f INDF1, 7
- return
-
-;----------
-m_ch_setreadwrite
- mov_fw ch
- bra_z m_ch_startcrashread
- mov_wf readwrite
- return
-
-;----------
-m_ch_crashsetown
- mov_fw ch
- goto panic_crashread_setpointer
-
-;----------
-m_ch_readown
- bc_f ch, 4 ; ch is now our byte counter
-m_ch_readown_loop
- mov_fw POSTINC1
- call serial_write_char
- dec_f_ifnz ch
- bra m_ch_readown_loop
- return
-
-;----------
-m_ch_startcrashread
- bs_f mode,mode_crashread
- mov_lfsr 0x0,1
- return
-
-;----------
-m_ch_comma
- goto i2cm_read_another
-
-;----------
-m_ch_stop
- goto i2cm_read_done
-
-;----------
-m_ch_plus
- pin_l p0_booster_shutdown
- pin_l p0_booster_dirn
- pin_h p0_booster_pwm
- pin_h p0_cdu_enable
- return
-
-;----------
-m_ch_dollar
- bs_f mode,mode_readhex
- return
-
-;----------
-m_ch_digit
-; W now contains intended slave number
- bt_f_if0 readwrite,7 ; reading?
- bra i2cm_read_start
- ; writing, eh ?
-
- mov_lfsr 0x200, 2
- bra i2cm_write_start ; positive
-
-;----------
-m_ch_spc
- rcall m_buffer_reset
- clr_f readwrite
- return
-
-;----------
-m_ch_0
- rcall m_buffer_reset
- set_f readwrite
- return
-
-;----------
-m_buffer_reset
- clr_f FSR0L
-m_buffer_fix_fsr0h
- mov_lw 0x02
- mov_wf FSR0H
- return
-
-;----------------------------------------
-i2cmu_done
- mov_lw ' '
- bt_f_if1 readwrite, 7 ; ff is writing, so we ack
- bra serial_write_char
- tst_f_ifnz readwrite ; non-0 is automatic mode
- return ; no ack
- bra serial_write_char ; manual mode, we ack
-
-;----------
-i2cmu_read_got_byte
- rcall read_got_print
- tst_f_ifnz readwrite ; non-0 is automatic mode
- bra i2cmu_read_got_byte_automatic
- ; manual, we just return:
- return
-
-i2cmu_read_got_byte_automatic
- dec_f_ifnz readwrite
- bra i2cm_read_another
- ; now we'd be in manual mode
- inc_f readwrite ; we leave ourselves in automatic mode, count=1
- bra i2cm_read_done
-
-;----------
-read_got_print
- bt_f_if0 mode,mode_readhex
- bra serial_write_char
- bra serial_write_hex
-
-;----------
-i2cmu_write_next_byte
- mov_fw FSR0L
- xor_wfw FSR2L
- bt_f_if1 STATUS,Z
- return ; end of message
- ; no, not end:
- mov_fw POSTINC2
- bc_f STATUS,Z
- return
-
-;----------
-panic_kill_hook
- bt_f_if0 idloc1,idloc1_master
- return
- ; master:
- pin_h p0_booster_shutdown
- pin_l p0_cdu_enable
- return
-
-;======================================================================
-
-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
-; cribbed from iwjpictest/harness.asm and now mangled
-; so that it is interrupt-driven for receiving but
-; polling for transmission
-
-;----------------------------------------
-serial_setup
-; W undefined undefined
- mov_lw (1<<TXEN) | serial_brgh | (1<<TRMT)
- mov_wf TXSTA ; asynch xmit enabled, high baud rate?, 8-bit,
- movlw_movwf_spbrg
-serial_receive_reset ;from serial_read_if_error
- mov_lw (1<<SPEN) | (1<<CREN)
- mov_wf RCSTA ; enable serial port, continuous rx, 8-bit
- bc_f IPR1, RCIP ; low priority
- bs_f PIE1, RCIE ; interrupt enable
- return
-
-;----------------------------------------
-serial_intrl
-; on errors, sets LED red and transmits *
- bt_f_if0 PIR1, RCIF
- return
- rcall serial_intr_handle
- intrl_handled_nostack
-
-serial_intr_handle
- mov_fw RCSTA
- and_lw (1<<CREN) | (1<<FERR) | (1<<OERR)
- xor_lw (1<<CREN)
- bra_nz serial_read_if_error
- mov_fw RCREG
- goto serialu_read_char
-
-serial_read_if_error
- mov_lw '*'
- rcall serial_write_char
- rcall serial_receive_reset
- return
-
- include final.inc
-
- end