;Low-level USB peripheral routines include "settings.inc" include "ti83plus.inc" include "equates.inc" SEGMENT Main GLOBALS ON EXTERN DebugStart,DebugStop,WaitTimerBms,putAtoHL_ExtraRAMPage,getAfromHL_ExtraRAMPage InitializePeriphUSB: ;Initializes ourselves as a USB peripheral connected to...something ;Returns carry set if problems ;This routine can be replaced with an entry point...possibly 5311h (relies on interrupt) ld a,80h out (57h),a xor a out (4Ch),a ld a,1 out (5Bh),a xor a in a,(4Ch) ld a,2 out (54h),a ld a,20h out (4Ah),a call setPowerPorts ld a,8 out (4Ch),a ld de,0FFFFh $$: call decDEcounter scf ret z in a,(4Ch) cp 1Ah jr z,$F cp 5Ah jr nz,$B $$: ld a,0FFh out (87h),a xor a out (92h),a in a,(87h) ld a,0Eh out (89h),a ld a,5 out (8Bh),a in a,(81h) or 1 out (81h),a in a,(54h) or 1 out (54h),a ld b,5 frameCounterLoop: ld de,0FFFFh $$: call decDEcounter jr z,counterExpired in a,(8Ch) or a jr z,$B or a ret counterExpired: djnz frameCounterLoop scf ret setPowerPorts: xor a out (4Bh),a in a,(3Ah) bit 3,a jr z,$F ld a,20h out (4Bh),a $$: xor a out (54h),a ld b,1 call WaitTimerBms in a,(3Ah) bit 3,a jr z,$F ld a,44h out (54h),a $$: ld a,0C4h out (54h),a ret RecycleUSB: ;Disconnects the USB connection and then re-initializes it ;Returns carry set if problems ;This routine can be replaced with an entry point...possibly 5311h (relies on interrupt) xor a out (5Bh),a in a,(4Dh) bit 5,a jr nz,$F xor a out (4Ch),a res 6,(iy+41h) jr finishPart1 $$: ld b,8 in a,(4Dh) bit 6,a jr nz,$F ld b,0 $$: ld a,b out (4Ch),a finishPart1: ld a,2 out (54h),a ld a,(39h) and 0F8h out (39h),a res 0,(iy+41h) in a,(4Dh) bit 5,a jr nz,finishPart2 ld de,0FFFFh $$: dec de ld a,d or e scf ret z in a,(4Dh) bit 7,a jr z,$B in a,(4Dh) bit 0,a jr z,$B ld a,22h out (57h),a xor a ret finishPart2: in a,(4Dh) bit 6,a jr nz,$F xor a out (4Ch),a ld a,50h jr outputPort57h $$: ld a,93h outputPort57h: out (57h),a xor a ret SetDeviceAddress: out (80h),a ld (USBaddress),a ret GetControlPacket: ;Gets B bytes from control pipe to HL in a,(0A0h) ld (hl),a inc hl djnz GetControlPacket ret WaitPort82: ;Wait on port 82h to acknowledge transfer in a,(82h) and 1 jr z,WaitPort82 in a,(91h) ret SetupOutPipe: ;Sets up outgoing pipe ;Inputs: A is max packet size / 8 out (90h),a ld a,48h out (91h),a xor a out (92h),a ret SetupInPipe: ;Sets up incoming pipe ;Inputs: A is max packet size / 8 out (93h),a ld a,90h out (94h),a xor a out (95h),a ret SendInterruptData: ;Sends B bytes at HL to interrupt pipe C ;Returns carry if problems set indicOnly,(iy+indicFlags) ld a,c ld (asm_ram),a ld c,0A0h add a,c ld c,a push bc ld de,0FFh $$: call decDEcounter scf pop bc jr z,sendInterruptDataDone push bc in a,(8Eh) ld c,a ld a,(asm_ram) out (8Eh),a in a,(91h) ld b,a ld a,c out (8Eh),a ld a,b and 1 jr nz,$B pop bc $$: ld a,(hl) out (c),a inc hl djnz $B in a,(8Eh) ld c,a ld a,(asm_ram) out (8Eh),a ld a,1 out (91h),a ld a,c out (8Eh),a xor a sendInterruptDataDone: res indicOnly,(iy+indicFlags) ret decDEcounter: dec de ld a,d or e ret scfRet: scf ret ReceiveInterruptData: ;Receives B bytes to HL on bulk pipe 02, max size 08h ld a,b or a ret z ld a,8 cp b ret c ld a,b ld (9C80h),a ld a,(bytesRemaining) or a jr z,$F cp b jr nc,$F ld b,a ld (9C80h),a $$: ld a,2 out (8Eh),a in a,(94h) bit 6,a jr z,$F and 0DFh out (94h),a pop af scf ret $$: ld a,(bytesRemaining) or a jr nz,$F in a,(96h) $$: push af ld c,0 ridLoop: in a,(0A2h) ld (hl),a inc hl inc c djnz ridLoop ld a,2 out (8Eh),a pop af sub c ld (bytesRemaining),a ret nz xor a ld (bytesRemaining),a in a,(94h) and 0FEh out (94h),a res 5,(iy+41h) ld a,21h out (8Bh),a ret