Ok, based on what you told me, I've made some more changes:
;----------------------------------------
; This code will be in the firmware somewhere...
FirmwareCode:
; load the data into the dma buffer
mov A, [Test0]
mov [SendOut0], A
mov A, [Test1]
mov [SendOut1], A
mov X, 02h ; 2 bytes of data pre-loaded
call SendData
; do other things...
nop
nop
ret
;------------------------------------
; Endpoint 1 Processing
USB_EP1_ISR:
push A ; save accumulator on stack
iord EP_A1_Mode ; test whether we have an ACK bit
and A, ACK_BIT
jz doneEP1 ; do nothing if we don't have an ACK bit
mov A, NAKIN ; clear ACK bit
iowr EP_A1_Mode
iord EP_A1_Counter ; flip data 0/1 bit after
xor A, DATATOGGLE ; a successful data transfer
iowr EP_A1_Counter
doneEP1:
pop A ; restore accumulator from stack
reti ; return from interrupt
; ------------------------------------------------
; Called when firmware needs to send data to host
; Pass X=Number of bytes preloaded into EP1_Data bytes
; Returns X=0 if busy, X=unchanged if ok
SendData:
push A
push X
; If busy, exit with X=0
iord EP_A1_Mode
and A, ACK_IN
cmp A, ACK_IN
jnz SendData_Busy
; Update the counter with the number of bytes to send
mov A, X
mov [tmp], A
iord EP_A1_Counter
or A, [tmp] ; tmp contains number of bytes to send
iowr EP_A1_Counter
; Transfer the data from the buffer into the endpoint FIFO
mov X, 00h
SendData_Loop_Start:
mov A, [SendOut0 + X]
mov [ep1_dmabuff0 + X], A
inc X
mov A, X
cmp A, 08h
jnz SendData_Loop_Start
SendData_Loop_End:
; Set the endpoint mode to ACK_IN to kick start the transmission
mov A, ACK_IN
iowr EP_A1_Mode
; all done, all ok
pop X
pop A
ret
SendData_Busy:
; Set X=0 to indicate that nothing was sent
pop X
mov X, 00h
pop A
ret
So this time I have a buffer between Test(0-7) and ep1_dmabuff(0-7) called SendOut(0-7) which loads the test data only after it knows it can.
The next thing I changed was the part which updates the counter in the SendOut function. Now EP_A1_Counter's state is merely updated with the count, which I presume will preserve the state of the toggle bit?
Let me know if you see anything else I should add to this.
And again THANK YOU SO MUCH!!