From 4683ec2f2513651e8619dd3314987f6b9f456f54 Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 13 Feb 2005 22:51:53 +0000 Subject: [PATCH] fixes and work in progress for I2C debugging --- cebpic/Makefile | 3 +- cebpic/README.protocol | 4 +- cebpic/i2c-test.asm | 83 +++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/cebpic/Makefile b/cebpic/Makefile index d3cfc90..3bb9809 100644 --- a/cebpic/Makefile +++ b/cebpic/Makefile @@ -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 diff --git a/cebpic/README.protocol b/cebpic/README.protocol index 711ce28..d066005 100644 --- a/cebpic/README.protocol +++ b/cebpic/README.protocol @@ -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 diff --git a/cebpic/i2c-test.asm b/cebpic/i2c-test.asm index a7c711d..3ad30c1 100644 --- a/cebpic/i2c-test.asm +++ b/cebpic/i2c-test.asm @@ -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 -- 2.30.2