
;******************************
;*
;* i2cSend8WACK
;*
;* r1 = data
;* r6 = UcI2CSoftPortA/BCtrl
;* 
;* return:
;*
;* r0 = 0 (OK) else other
;*

i2cSend8WACK

	push	r1
	push	r7
	push	r5

	;76543210
	;      

	; clk down
	
	movei	r5,7	;8 times

i2cSend8WACKPushLoop	
	
	;DIR OUT, CLK 0
	
	movei	r0,$4	
	extri	r1,7
	addtqi	r0,r0,0	;set r0 bit 0 = 1 if bit 7 in r1 = 1
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$7		;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH	;delay slot
	nop			;delay slot
	
	;CLK = 1
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	addi	r0,2		;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH	;delay slot
	nop			;delay slot
	
	subi	r5,1
	brts	i2cSend8WACKPushLoop
	add	r1,r1,r1	;next bit	delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;bit send so wait for ack
	;DIR IN, CLK 0
	
	movei	r0,$0	;DIR IN, CLK 0
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07	;EN A		delay slot
	nop		;delay slot
	st	r6,r0	;PUSH		delay slot
	nop		;delay slot
	
	;push clk high
	
	movei	r0,$2	;DIR IN, CLK 1

	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH			;delay slot
	nop			;delay slot
	
	;get result
	
	rqldi	r6,0
	nop
	movei	r1,$8
	ld	r0		
	nop
	and	r0,r0,r1	;r0 = 0 OK, else fail
	
	;clk down again (default)
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	movei	r1,$0	;DIR IN, CLK 0
	nop
	moveih	r1,$07	;EN A
	nop
	st	r6,r1	;PUSH
	
	
	rqpop
	rqpop	
	rqpop
	pop	r5
	pop	r7
	pop	r1
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;*
;* i2cRecive8
;*
;* r6 = UcI2CSoftPortA/BCtrl
;* 
;* return:
;*
;* r0 = data
;*

i2cRecive8

	push	r1
	push	r7
	push	r5
	
	movei	r0,0

	;76543210
	;      

	; clk down
	
	movei	r5,7	;8 times

i2cRecive8PushLoop	
	
	movei	r1,$0	;DIR IN, CLK 0
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r1,$7		;delay slot
	nop			;delay slot
	st	r6,r1		;PUSH			;delay slot
	nop			;delay slot
	

	gpci	r7,2		;next 4 + X
	br	i2cWaitHalfPeriode
	addi	r1,2		;CLK = 1		;delay slot
	nop			;delay slot
	st	r6,r1		;PUSH			;delay slot
	nop			;delay slot
	
	rqldi	r6,0
	nop	
	movei	r7,$1
	ld	r1
	add	r0,r0,r0	;r0 * 2
	lsri	r1,3
	
	subi	r5,1
	brts	i2cRecive8PushLoop
	and	r1,r1,r7	;delay slot
	nop			;delay slot
	or	r0,r0,r1	;delay slot
	add	r1,r1,r1	;next bit		;delay slot
	
	nop
	movei	r1,$0	;DIR IN, CLK 0

	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r1,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r1		;PUSH			;delay slot
	nop			;delay slot
	
	rqpop
	rqpop	
	rqpop
	pop	r5
	pop	r7
	pop	r1
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot	
	
;******************************
;*
;* i2cSendStart
;*
;* r6 = UcI2CSoftPortA/BCtrl
;
i2cSendStart

	push	r0
	push	r7
	
	movei	r0,$7	;DIR OUT, CLK 1, DATA 1
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH			;delay slot
	nop			;delay slot
	
	movei	r0,$6	;DIR OUT, CLK 1, DATA 0

	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH			;delay slot
	nop			;delay slot
	
	rqpop
	rqpop
	nop
	pop	r7
	pop	r0
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop	
	
;******************************
;*
;* i2cSendStop
;*
;* r6 = UcI2CSoftPortA/BCtrl
;
i2cSendStop

	push	r0
	push	r7
	
	movei	r0,$6	;DIR OUT, CLK 1, DATA 0

	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH			;delay slot
	nop			;delay slot
	
	movei	r0,$7	;DIR OUT, CLK 1, DATA 1
	
	gpci	r7,2	;next 4 + X
	br	i2cWaitHalfPeriode
	moveih	r0,$07		;EN A			;delay slot
	nop			;delay slot
	st	r6,r0		;PUSH			;delay slot
	nop			;delay slot
	
	rqpop
	rqpop
	nop
	pop	r7
	pop	r0
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop	
	
;******************************
;*
;* i2cWaitHalfPeriode
;*	
	
i2cWaitHalfPeriode

	push	r0

	;min 134 cycles to waste
	
	movei	r0,20		;21 * 6 = 126 (+9 cycles) => 135
	nop
	
i2cWaitHalfPeriodeLoop	
	subi	r0,1
	brts	i2cWaitHalfPeriodeLoop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	rqpop
	jmpi	r7,0
	nop			;delay slot
	pop	r0		;delay slot
	nop			;delay slot
	nop			;delay slot	