From: ceb Date: Mon, 17 Jan 2005 00:04:08 +0000 (+0000) Subject: * added serial error checking to nmra-stream (panics if overrun or X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=ed5ad57ae8ad2f7eac7c09d8819fc5f833a2c52e;p=trains.git * added serial error checking to nmra-stream (panics if overrun or framing error bits set) * skeleton for 12c-test --- diff --git a/cebpic/i2c-test.asm b/cebpic/i2c-test.asm new file mode 100644 index 0000000..901e6cb --- /dev/null +++ b/cebpic/i2c-test.asm @@ -0,0 +1,185 @@ + +; 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 + + +; reserved for NMRA: +NMRACTRL equ 0x4 ; byte 4: state relevant to NMRA control +TRANSMITBIT equ 0x7 ; bit 7: 0/1 bit currently being transmitted +NEXTACTION equ 0x6 ; bit 6: change polarity on next interrupt y/n + +FROMSERIAL equ 0x1 ; byte 1: from-serial buffer location (in BSR5) +TOTRACK equ 0x2 ; byte 2: to-track buffer location (in BSR5) +TOTRACKBIT equ 0x3 ; byte 3: bit location of pointer within byte + +BUFFERPAGE equ 5 + + + ifdef SLOW_VERSION + messg "hello this is the slow version" + endif + + ifndef SLOW_VERSION + messg "hello this is the fast version" + endif + + ifdef SLOW_VERSION + messg "with an if" + else + messg "and an else" + endif + + org 0 + goto initialise + +;**************************************************************************** + +; high priority interrupt + + org 000008h + goto interrupt_high + +; low priority interrupt + + org 000018h + goto interrupt_low + +;**************************************************************************** + + code + +;**************************************************************************** + +macros + +; macro to call subroutine to transmit over serial port for debugging +; takes 8-bit value, puts in W, invokes debug_serial_transmit + + ifndef SLOW_VERSION +debug macro debugvalue + endm + endif + + ifdef SLOW_VERSION +debug macro debugvalue + movlw debugvalue + call debug_serial_transmit + endm + endif + +debug_serial_transmit + movwf TXREG,0 ; move contents of W (i.e. debugvalue) + ; to TXREG for transmission +waitfortsr + btfss TXSTA,1,0 + bra waitfortsr + return + +;**************************************************************************** + +initialise + +; serial set-up + +; initial config - TXSTA register p181 + bcf TXSTA,6,0 ; p181, set 8-bit mode + bsf TXSTA,5,0 ; transmit enable + bcf TXSTA,4,0 ; asynchronous mode + bsf TXSTA,2,0 ; set high baud rate + +; initial config - RCSTA register p182 + bsf RCSTA,7,0 ; serial port enable (p182) + bcf RCSTA,6,0 ; 8-bit reception + bsf RCSTA,4,0 ; enable continuous receive + +; set SPBRG to get correct baud rate according to table top right p186 +; (Tosc = 20MHz, desired baud rate = 9600) + bsf SPBRG,7,0 + bsf SPBRG,0,0 + + +; interrupt set-up for serial receive + bcf IPR1,5,0 ; set to low-priority interrupt + + +;---------------------------------------------------------------------------- + +; interrupt set-up + +; globally enable interrupts - p77 + bsf RCON,7,0 ; enable priority levels + bsf INTCON,7,0 ; enable high-priority interrupts + bsf INTCON,6,0 ; enable low-priority interrupts + bsf PIE1,5,0 ; enable USART receive interrupt (p85) + + +;**************************************************************************** + + call led_green +main_loop_led + goto main_loop_led + + +;**************************************************************************** + +interrupt_high + debug 'H' + goto panic + + +;**************************************************************************** + +interrupt_low + +; check which interrupt. + debug 'L' + goto panic +; serial only? + + +;**************************************************************************** + +serial_receive + debug 'S' + goto panic +; serial byte to say pic #, LED flash pattern, look pattern up in table? +; (to test use of flash memory and lookup therein) +; think about serial msg format (in general and (simpler) for here) + + +;**************************************************************************** + + +led_green + bcf TRISD,2,0 ; make pin RD2 an output (DS100) + bsf LATD,2,0 ; set pin RD2 H (green) + return + +led_black + bsf TRISD,2,0 ; make pin RD2 an input (i.e. set Z, black) + ; (DS100) + return + +led_red + bcf TRISD,2,0 ; make pin RD2 an output (DS100) + bcf LATD,2,0 ; set pin RD2 L (red) + return + + +panic + debug 'x' + clrf INTCON,0 ; disable all interrupts EVER + debug 'y' + bcf PORTC,1,0 ; switch off booster + debug 'z' + call led_red +panic_loop + goto panic_loop + +;**************************************************************************** + + end diff --git a/cebpic/nmra-stream.asm b/cebpic/nmra-stream.asm index 81ba119..a5274d8 100644 --- a/cebpic/nmra-stream.asm +++ b/cebpic/nmra-stream.asm @@ -228,6 +228,12 @@ interrupt_low serial_receive ; debug 'h' ; write 'h' to serial port + btfsc RCSTA,FERR,0 ; if FERR set (= framing error), then panic + goto panic + btfsc RCSTA,OERR,0 ; if OERR set (= overrun error), then panic + goto panic + + btfsc RCSTA,OERR,0 ; OERR set = overrun error; panic movff FROMSERIAL,FSR0L ; set low byte of INDF0 pointer movlw BUFFERPAGE movwf FSR0H,0 ; set high byte of INDF0 pointer diff --git a/detpic/nmra-stream.asm b/detpic/nmra-stream.asm index 81ba119..a5274d8 100644 --- a/detpic/nmra-stream.asm +++ b/detpic/nmra-stream.asm @@ -228,6 +228,12 @@ interrupt_low serial_receive ; debug 'h' ; write 'h' to serial port + btfsc RCSTA,FERR,0 ; if FERR set (= framing error), then panic + goto panic + btfsc RCSTA,OERR,0 ; if OERR set (= overrun error), then panic + goto panic + + btfsc RCSTA,OERR,0 ; OERR set = overrun error; panic movff FROMSERIAL,FSR0L ; set low byte of INDF0 pointer movlw BUFFERPAGE movwf FSR0H,0 ; set high byte of INDF0 pointer