chiark / gitweb /
fixes and work in progress for I2C debugging
authorian <ian>
Sun, 13 Feb 2005 22:51:53 +0000 (22:51 +0000)
committerian <ian>
Sun, 13 Feb 2005 22:51:53 +0000 (22:51 +0000)
cebpic/Makefile
cebpic/README.protocol
cebpic/i2c-test.asm

index d3cfc90d143eff792d9db0540011ada09d5570eb..3bb98091680ab90650c34a57edba52cbb9730974 100644 (file)
@@ -1,5 +1,6 @@
 PROGRAMS=      led-flash send-serial reply-serial      \
-               nmra-stream nmra-stream,slow tblrd_test i2c-test
+               nmra-stream nmra-stream,slow tblrd_test \
+               i2c-test i2c-test,slow
 # booster-output-low  -disabled because you forgot to cvs add it
 
 PICNOS=                0 1 3
index 711ce280125e6cb0605f6a4b30068641b1a95b83..d0660057f09ba4637f6afa79445ecc614cd531b4 100644 (file)
@@ -28,9 +28,9 @@ Byte 20 0000h
                
 
 Byte 20 0001h
-               bit 0           1 for the main PIC (#0)
+               bit 7           1 for the main PIC (#0)
                                0 otherwise
-               1-7             currently unused, set to 0
+               0-6             currently unused, set to 0
 
 
 I2C
index a7c711d3298cfad170656d35361ab4e2c0a0ce69..3ad30c1668c5a75076dfdbee251786b24ecc91cc 100644 (file)
@@ -17,6 +17,7 @@
 ; labels ending _loop are for loops
 ; labels ending _isr are at the start of interrupt service routines
 ;                      (which must end with retfie)
+; labels starting vector_ are the reset and interrupt entrypoints
 ; other labels in lowercase are normal subroutines (ending in `return')
 ; labels in UPPERCASE are defined addresses (in RAM or flash)
 
@@ -55,7 +56,7 @@ F_I2C_CTRL    equ     0x200001
 PIC_NO         res     1
 
 I2C_CTRL       res     1
-I2C_CTRL_MASTER        equ     0       ; bit 0 of I2C_CTRL is 1=master 0=slave
+I2C_CTRL_MASTER        equ     7       ; bit 7 of I2C_CTRL is 1=master 0=slave
 
 ;****************************************************************************
 ; VECTORS: special locations, where the PIC starts executing
@@ -74,7 +75,7 @@ I2C_CTRL_MASTER       equ     0       ; bit 0 of I2C_CTRL is 1=master 0=slave
 ; MACROS
 
 ;----------------------------------------
-; debugvalue(BYTE)
+; debug(BYTE)
 ;      writes BYTE through the serial port
 ;      serial port hardware must be suitably initialised
 ;      serial port transmit interrupts must be disabled
@@ -93,27 +94,10 @@ debug macro debugvalue
        ifdef   SLOW_VERSION
 debug macro debugvalue
        movlw   debugvalue
-       call    debug_serial_transmit
+       call    polling_serial_transmit
        endm
        endif
 
-;--------------------
-debugvalue_serial_transmit
-;      writes W through the serial port
-;      for use by debugvalue macro only
-
-       movwf   TXREG,0         ; move contents of W (i.e. debugvalue)
-                               ;       to TXREG for transmission
-debugvalue_waitfortsr_loop
-       btfss   TXSTA,1,0
-       bra     debugvalue_waitfortsr_loop
-
-       return
-
-;****************************************************************************
-
-       code
-
 ;****************************************************************************
 ; PORTMANTEAU CODE
 ; which contains lists of checks and calls to function-specific
@@ -121,10 +105,20 @@ debugvalue_waitfortsr_loop
 
 ;----------------------------------------
 vector_reset
+
        call    serial_setup
+
+       debug   'a'
+
        call    copy_per_pic_data
+
+       debug   'b'
+
        call    i2c_setup
        call    enable_interrupts
+
+       debug   'c'
+
        goto    main
 
 ;----------------------------------------
@@ -193,16 +187,28 @@ slave_main
 slave_main_loop
        call    wait_for_i2c_interrupt  ; wait for 1st (address) byte
        call    wait_for_i2c_interrupt  ; wait for 2nd (data) byte
-       btg     LATD,2,0                ; toggle colour of LED
+       btg     TRISD,2,0               ; toggle LED on/off
        goto    slave_main_loop
 
 ;----------------------------------------
 serial_rx_isr
+       call    led_black
+
+       debug   'd'
+
 ;      what we actually do here is faff with I2C to start transmitting
        bsf     SSPCON2,SEN,0           ; i2c START
        call    wait_for_i2c_interrupt
+
+       debug   'e'
+
        movlw   0x41                    ; transmit address 0100 0001
+       movwf   SSPBUF,0
+
        call    wait_for_i2c_interrupt
+
+       debug   'f'
+
        btfss   SSPCON2,ACKSTAT,0       ; check for ack from slave (=0), if no
        goto    panic                   ; then panic, else
        movff   RCREG,SSPBUF            ; copy byte from serial to i2c buffer
@@ -212,8 +218,7 @@ serial_rx_isr
        bsf     SSPCON2,PEN,0           ; i2c STOP
        call    wait_for_i2c_interrupt
 
-;!!!fixme - next line is wrong, surely ?
-       goto    master_main
+       retfie
 
 ;***************************************************************************
 ; SERIAL PORT
@@ -243,6 +248,25 @@ serial_setup
        bcf     IPR1,5,0        ; set to low-priority interrupt
        return
 
+;--------------------
+polling_serial_transmit
+;      writes W through the serial port
+;      serial port hardware must be suitably initialised
+;      serial port transmit interrupts must be disabled
+;      will spin until the byte is transmitted
+
+       movwf   TXREG,0         ; move contents of W (i.e. debugvalue)
+                               ;       to TXREG for transmission
+debug_waitfortsr_loop
+       btfss   TXSTA,1,0
+       bra     debug_waitfortsr_loop
+
+       return
+
+;****************************************************************************
+
+       code
+
 ;***************************************************************************
 ; FLASH ID LOCATIONS
 
@@ -255,7 +279,7 @@ copy_per_pic_data
        movwf   TBLPTRU                 ; PIC number in flash
        movlw   (F_PIC_NO >> 8) & 0xff
        movwf   TBLPTRH
-       movlw   F_PIC_NO
+       movlw   F_PIC_NO & 0xff
        movwf   TBLPTRL
 
        tblrd   *+              ; read then increment pointer 
@@ -265,6 +289,9 @@ copy_per_pic_data
         movf    TABLAT,0,0      ; move pic number into normal memory
         movwf   PIC_NO,1
 
+       iorlw   '0'
+       call    polling_serial_transmit
+
         tblrd   *               ; read i2c
 
         movf    TABLAT,0,0      ; move i2c_ctrl byte into normal memory
@@ -272,6 +299,9 @@ copy_per_pic_data
 ; now have: PIC number in 400h, i2c control byte in 401h - see
 ; RAM variables re i2c specific stuff, above
 
+       iorlw   '0'
+       call    polling_serial_transmit
+
        return
 
 ;***************************************************************************
@@ -299,7 +329,7 @@ i2c_setup
        bcf     IPR1,SSPIP,0    ; make interrupt low priority
 
 ; are we master or slave ?
-       btfss   I2C_CTRL,I2C_CTRL_MASTER,1      ; test whether PIC is master
+       btfss   I2C_CTRL,I2C_CTRL_MASTER        ; test whether PIC is master
        goto    i2c_setup_if_master
        goto    i2c_setup_if_slave
 
@@ -335,7 +365,7 @@ i2c_setup_endif_master_slave
 ;----------------------------------------
 i2c_isr
        banksel PIC_NO                  ; ser BSR=i2c BSR (4)
-       btfsc   I2C_CTRL,I2C_CTRL_MASTER ; check =master?, if so
+       btfsc   I2C_CTRL,I2C_CTRL_MASTER,1 ; check =master?, if so
        goto    i2c_master_isr          ; goto master interrupt routine
        goto    i2c_slave_isr           ; elso goto interrupt_slave
 
@@ -356,6 +386,7 @@ wait_for_i2c_interrupt
 ;      are enabled, clearly.
 
 wait_for_i2c_interrupt_loop
+       debug   '.'
        btfss   PIR1,SSPIF,0    ; check if interrupt set, if not, loop
        goto    wait_for_i2c_interrupt_loop