📄 usbasm.txt
字号:
;*******************************************************************************
; File: HC12 Interface2 (filename: usbasm.asm)
;
; Purpose:
; 1. Receive/send data from/to HC12 by HAPI
; 2. Drive printer
; 3. Receive/send data from/to PC by USB
;
; Port Signal Definines:
; 1. Printer:
; P3.bit[7:0] as Printer Data Port
; Input: DAC.bit[0] as *ERROR, DAC.bit[1] as BUSY, DAC.bit[2] as *ACK
; Output: P2.bit[0] as *INIT, P2.bit[1] as *AUTPFD, P2.bit[7] as *STROBE
;
; 2. HAPI:
; P0.bit[7:0] as HAPI Data Bus
; P1.bit[2:0] as Address Bus (read only)
; Input: P2.bit[4] as *STB, P2.bit[5] as *RD, P2.bit[6] as *CS
; Output: P2.bit[2] as *LMPTY (IRQ2), P2.bit[3] as *DRDY (IRQ1)
;
; USB Endpoints Definines:
; 1. Endpoint0: control endpoint
; 2. Endpoint1: Bulk IN mode (Device->Host)
; 3. Endpoint2: Bulk OUT mode (Host->Device)
;
;
;******************************************************************************
; Version Notes: Version 1.32
;
;
; 1. Added HAPI_int interrupt handler to receive/send
; data from/to HC12 and parallel printer.
;
; 2. Modified MainLoop.
;
; 3. Modified HAPI_int and MainLoop to match NEW LPT/USB
; interface protocol.
;
; 4. The first version(1.0) be burned into silicon chip.
;
; 5. Added function to check USB status.
;
;
; 6. Fixed the BUG found at put_send_buffer Function,
; modified HAPI_int (byte_2) and send_to_HC12 Function.
; ( Version 1.1)
;
; 7. Fixed the watch-dog reset( when USB cable plug-in) problem
; ( Version 1.2)
;
; 8. Changed EP1 to IN mode and EP2 to OUT mode.
; ( Version 1.3)
;
; 9. Put received HC12 data to endpoint1 buffers directly, no send_data routine.
; Disable EP1 and EP2 Interrupts after IN or OUT packet sending or receiving.
; (Version 1.31)
;
; 10. Add bit7 UREADY on BYTE4 STATUS Register to indicate if this firmware version
; supports USB or not (add at Aug,20 2001) This bit7 is always 0 on previous
; firmware versions. ( Version 1.32)
;
;
; Software #: 90-007-0025
; Firmware #: 90-207-0100
;
;*****************************************************************************
;**************** Assembler Directives ***************************************
lable:
CPU 64113
XPAGEON
; processor registers
Port0: equ 00h
Port1: equ 01h
Port2: equ 02h
Port3: equ 03h
Port0_int: equ 04h
Port1_int: equ 05h
Port2_int: equ 06h
Port3_int: equ 07h
GPIO_cfg: equ 08h
HAPI_I2C_cfg: equ 09h
USB_address: equ 10h
EP0_counter: equ 11h
EP0_mode: equ 12h
EP1_counter: equ 13h
EP1_mode: equ 14h
EP2_counter: equ 15h
EP2_mode: equ 16h
EP3_counter: equ 41h
EP3_mode: equ 42h
EP4_counter: equ 43h
EP4_mode: equ 44h
USB_ctl: equ 1fh
Global_int: equ 20h
EP_int: equ 21h
Int_vec: equ 23h
Timer_lo: equ 24h
Timer_hi: equ 25h
WDT: equ 26h
I2C_ctl: equ 28h
I2C_data: equ 29h
DAC_data: equ 30h
;DAC_int: equ 31h
;DAC_intp: equ 32h
CPU_ctl: equ 0ffh
;====================================================
; RAM MAP:
; -------------------------------------------
; | 00->xx: reserve for PSP (xx<20) |
; | xx->20: reserve for DSP (xx<20) 32 bytes |
; | 21->3F: 32 bytes Data Memory Variables |
; | 40->5F: 32 bytes received data from PC |
; | 60->7F: 32 byets send_data to PC |
; | A8->FF: ENDPOINT FIFOs |
; -------------------------------------------
;====================================================
; Endpoint Buffer Allocation (USB_ctl bit[7:6]=[1,0])
EP0_buffer0: equ 0b8h
EP0_buffer1: equ 0b9h
EP0_buffer2: equ 0bah
EP0_buffer3: equ 0bbh
EP0_buffer4: equ 0bch
EP0_buffer5: equ 0bdh
EP0_buffer6: equ 0beh
EP0_buffer7: equ 0bfh
EP1_buffer0: equ 0c0h ; total 32 bytes 0C0-0DF
EP1_buffer1: equ 0c1h
EP1_buffer2: equ 0c2h
EP1_buffer3: equ 0c3h
EP1_buffer4: equ 0c4h
EP1_buffer5: equ 0c5h
EP1_buffer6: equ 0c6h
EP1_buffer7: equ 0c7h
; ...
EP1_buffer31: equ 0dfh
EP2_buffer0: equ 0e0h ; total 32 bytes 0E0-0FF
EP2_buffer1: equ 0e1h
EP2_buffer2: equ 0e2h
EP2_buffer3: equ 0e3h
EP2_buffer4: equ 0e4h
EP2_buffer5: equ 0e5h
EP2_buffer6: equ 0e6h
EP2_buffer7: equ 0e7h
; ...
EP2_buffer31: equ 0ffh
; USB Mode Encoding
disabled: equ 00h
nak: equ 01h
stall: equ 03h
ignore: equ 04h
con_rd_ack: equ 0Fh
con_rd_nak: equ 0Eh
con_rd_stall: equ 02h
con_wr_ack: equ 0Bh
con_wr_nak: equ 0Ah
con_wr_stall: equ 06h
out_ack: equ 09h
out_nak: equ 08h
out_iso: equ 05h
in_ack: equ 0Dh
in_nak: equ 0Ch
in_iso: equ 07h
; USB Stardard Request Types:
get_status: equ 00h
clear_feature: equ 01h
set_feature: equ 03h
set_address: equ 05h
get_descriptor: equ 06h
set_descriptor: equ 07h
get_configuration: equ 08h
set_configuration: equ 09h
get_interface: equ 0Ah
set_interface: equ 0Bh
synch_frame: equ 0Ch
get_deviceID: equ 00h ; printer device only
get_portstatus: equ 01h ; printer device only
device_status: equ 00h
endpoint_status: equ 00h
endpoint_stalled: equ 00h
device_remote_wakeup: equ 01h
; USB Standard Descriptor Types:
device: equ 01h
configuration: equ 02h
string: equ 03h
interface: equ 04h
endpoint: equ 05h
; USB Class Specific Descriptor Types
HID: equ 21h
report: equ 22h
physical: equ 23h
; USB Class Specific Request Codes
get_report: equ 01h
get_idle: equ 02h
get_protocal: equ 03h
set_report: equ 09h
set_idle: equ 0ah
set_protocal: equ 0bh
; BYTE Type for HAPI Port 1.bit[0.1.2] ref [A0.A1.A2]
HAPI_PDOUT: equ 00h ; HAPI parallel data out register address
HAPI_UDOUT: equ 01h ; HAPI USB data out register address
HAPI_UDIN: equ 02h ; HAPI USB data in register address
HAPI_CTRL: equ 03h ; HAPI control register address
HAPI_STAT: equ 04h ; HAPI Status register address
; Data bit for HAPI_STAT Register (printer and USB device status):
HAPI_PTDRE: equ 01h ; [0000,0001],bit0,parallel transmit data register empty
HAPI_UTDRE: equ 02h ; [0000,0010],bit1,usb transmit data register empty
HAPI_URDRF: equ 04h ; [0000,0100],bit2,usb receive data register full
HAPI_PBUSY: equ 08h ; [0000,1000],bit3,printer busy (physical device status)
HAPI_UBUSY: equ 10h ; [0001,0000],bit4,usb port busy(physical device status)
HAPI_PERROR: equ 20h ; [0010,0000],bit5,printer error(CY device status)
HAPI_UERROR: equ 40h ; [0100,0000],bit6,usb port error(CY device status)
HAPI_UREADY: equ 80h ; [1000,0000],bit7,usb port ready (firmware);;(add at Aug,20 2001)
HAPI_PRDY: equ 0d7h ; [1101,0111], bit5, bit3, printer ready
; Data bit for HAPI_CTRL Register (HC12 Request):
HAPI_PTIE: equ 01h ; [0000,0001],bit0,printer transmit data IRQ enable
HAPI_UTIE: equ 02h ; [0000,0010],bit1,usb transmit data IRQ enable
HAPI_URIE: equ 04h ; [0000,0100],bit2,usb receive data IRQ enable
HAPI_ULAST: equ 08h ; [0000,1000],bit3,usb last byte of message indicator(USB only)
HAPI_PENABLE: equ 10h ; [0001,0000],bit4,printer enable
HAPI_UEBANLE: equ 20h ; [0010,0000],bit5,usb enable
; Command Terminator (send by PC):
COMM_END: equ 80h ; command terminator
; USB Detect
BUS_activity_clean: equ 0f7h ; data for clean bus_activity bit [1111,0111] USB_ctl.bit3
; Data Memory Variables: (Space from 21h-3fh, 64 bytes)
delay_time: equ 21h ; 0ffh=390us (almost)
perror_counter: equ 22h ; counter for recording printer error times
pbusy_counter: equ 23h ; counter for recording printer busy times
;pre_setup: equ 24h
loop_counter: equ 25h
;bus_active: equ 26h
byte_type: equ 27h ; temp buffer for restoring Port1 data
EP0_data_toggle: equ 28h
EP1_data_toggle: equ 29h ; mod at July,26,2001
EP2_data_toggle: equ 2ah
data_start: equ 2bh ; USB RAM table start point
data_count: equ 2ch ; USB RAM table length
;EP2_recv: equ 2dh ; set by ep2, clear by mainloop
;counter_1: equ 2eh
endpoint_stall: equ 2fh
printer_RDY: equ 30h ; flag, set indicate that printer is not ready
send_data_ok: equ 31h ; flag, set indicate that received 32 bytes data from HC12
recvpc_data_ok: equ 32h ; flag, set indicate that received 32 bytes data from EP1
data_count_8: equ 33h ; flag, set indicate a mutiple 8 bytes packet
config_status: equ 34h ; USB configuration status
bus_reset_flag: equ 35h ; flag, set indicate a bus reset event.
temp_print_data: equ 36h ; temp buffer for storing printer data
;data_type: equ 37h ; HAPI data type
send_data_counter: equ 38h ; counter for counting data received from HC12
HC12_data_counter: equ 39h ; counter for counting data that need to send to HC12
;port1_temp: equ 3ah ; Port1 temp data
HAPI_CTRL_reg: equ 3bh ; HAPI Control Register
;HC12_write_flag: equ 3ch ; flag, set indicate that HC12 write data to port0
rcvdata_length: equ 3ch ; EP2 received data length
HAPI_STAT_reg: equ 3dh ; HAPI device status register
HAPI_UDIN_reg: equ 3eh ; HAPI USB data in register
; Data Transmission Buffers Allocation:
recvpc_data0: equ 40h ; (data length 32 bytes 40-5f)
recvpc_data1: equ 41h ; received from PC by EP1 and need be sent to HC12
recvpc_data2: equ 42h
recvpc_data3: equ 43h
recvpc_data4: equ 44h
recvpc_data5: equ 45h
recvpc_data6: equ 46h
recvpc_data7: equ 47h
;...
recvpc_data31: equ 5fh
send_data0: equ 60h ; (data length 32 bytes 60-7f)
send_data1: equ 61h ; received from HC12 and need be sent to PC by EP2
send_data2: equ 62h
send_data3: equ 63h
send_data4: equ 64h
send_data5: equ 65h
send_data6: equ 66h
send_data7: equ 67h
;...
send_data31: equ 7fh
;*************** interrupt vector table ****************
ORG 00h
jmp reset ; reset vector
jmp bus_reset ; USB bus reset interrupt
jmp unused_int ; 128-us interrupt (disable)
jmp 1ms_int ; 1.024-ms interrupt(watch dog reset)
jmp endpoint_zero ; endpoint 0 interrupt(control)
jmp endpoint_one ; endpoint 1 interrupt (IN Endpoint, Send data to PC)
jmp endpoint_two ; endpoint 2 interrupt (OUT Endpoint, Receive data from PC)
jmp unused_int ; endpoint 3 interrupt (disable)
jmp unused_int ; endpoint 4 interrupt (disable)
jmp error ; not implemented in this version
jmp unused_int ; DAC interrupt (disable)
jmp HAPI_int ; HAPI/GPIO interrupt
jmp unused_int ; I2C interrupt (dsiable)
;************** program listing ************************
ORG 1Ah
error: halt
;*******************************************************
; Interrupt handler: reset
; Purpose: The program jumps to this routine when
; the microcontroller has a power on reset.
;*******************************************************
reset:
mov A, 20h
swap A, dsp ; sets data memory stack pointer
mov A, 00h
;clear variable memory
mov X, 1fh
clear_ram_loop:
mov [X + recvpc_data0], A ; clear RAM
mov [X + send_data0], A
dec X
jnc clear_ram_loop
mov [EP0_data_toggle], A
;---------- mod at July,26,2001 ---------
mov [EP1_data_toggle], A
;---------- mod end at July,26,2001 ---------
mov [EP2_data_toggle], A
mov [data_start], A
mov [data_count], A
; mov [EP2_recv], A
; mov [counter_1], A
mov [loop_counter], A
mov [byte_type], A
mov [endpoint_stalled], A
; mov [bus_active], A
mov [send_data_ok], A
mov [recvpc_data_ok], A
mov [data_count_8], A
mov [config_status], A
mov [bus_reset_flag], A
mov [temp_print_data], A
; mov [data_type], A
mov [HC12_data_counter], A
mov [send_data_counter], A
; mov [port1_temp], A
mov [HAPI_CTRL_reg], A
; mov [HC12_write_flag], A
mov [rcvdata_length], A
mov [printer_RDY], A
mov [perror_counter], A
mov [pbusy_counter], A
mov [delay_time], A
mov [HAPI_UDIN_reg], A
mov A, 7bh ; init HAPI_STAT_reg register, [0111,1011]
or A, HAPI_UREADY ; This firmware version supports USB ;;(add at Aug,20 2001)
mov [HAPI_STAT_reg], A ; USB,printer BUSY,ERROR and PTDRE=1, UTDRE=1, URDRF=0
;----------------------------------------
; Initilization ports
;----------------------------------------
mov A, 0ch ; [0000,1100]00h
iowr GPIO_cfg ; set all ports to open-drain mode
mov A, 0ffh
iowr DAC_data ; disable DAC port output current sink
mov A, 0ffh
iowr Port1
;----------------------------------------
; HAPI Configuration
;----------------------------------------
iord HAPI_I2C_cfg ; set HAPI operation enable and 16 bit data width
and A, 0feh ; x[1111,1110], +[0011,0010]
or A, 32h ;32h ; set both DRDY and LEMPTY polariyh to 1
; DataReady bit [2]=1 (when Data is ready)
; LatchEmpty bit [3]=0 (when port is full)
iowr HAPI_I2C_cfg ; and causes printer control bit P2[0.1.7] CMOS out mode
;----------------------------------------
; USB Configuration
;----------------------------------------
; iord USB_ctl
; and A, 78h ; set Not Forcing control action bit[0:2]=[000] [0111,1000]
; or A, 40h ; set endpoint memory allocation bit[7,6]=[0,1] [0100,0000]
; iowr USB_ctl
iord USB_ctl
and A, 38h ; set Not Forcing control action bit[0:2]=[000] [0011,1000]
or A, 80h ; set endpoint memory allocation bit[7,6]=[1,0] [1000,0000]
iowr USB_ctl
;----------------------------------------
; Interrupts and CPU status_control Initilization
;----------------------------------------
; iord CPU_ctl
; and A, 0f7h ; set suspend bit[3]=0, [1111,0111]
; or A, 71h ; set WDG reset[6], BUS reset[5], PowerOn reset[4], RUN bit[0]=1, [0111,0001]
; iowr CPU_ctl
mov A, 25h ; enable USB_bus reset, 1ms and HAPI interrupts[0010,0101]
iowr Global_int
mov A, 07h ; enable EP0, EP1, EP2 interrupts [0000,0111]
iowr EP_int
ei ; enable interrupts
;-------------------------------------
; End of Reset
;-------------------------------------
; call prepare_EP
mov A, 00h ; clean EP0 FIFO buffers
mov [EP0_buffer0], A
mov [EP0_buffer1], A
mov [EP0_buffer2], A
mov [EP0_buffer3], A
mov [EP0_buffer4], A
mov [EP0_buffer5], A
mov [EP0_buffer6], A
mov [EP0_buffer7], A
mov A, [bus_reset_flag] ; verifiy USB BUS_Reset Flag set or not
cmp A, 01h ; to make sure that firmware can run even
jnz main ; without USB connection (just communicate with HC12)
mov A, 00h ; if BUS_Reset occured, So have to wait setup completed
mov [bus_reset_flag], A
waitconfig:
iord EP1_mode ; test if configured
and A, 0fh
cmp A, 00h
jz waitconfig
;*******************************************************
;*******************************************************
;** Main Loop **
;** Purpose: The main loop **
;*******************************************************
;*******************************************************
main:
call get_USB_status ; get USB bus Status
mov A, [recvpc_data_ok] ; check received data from EP1(PC) or not
cmp A, 01h ; flag was set by endpoint_two interrupt handler
jnz loop
mov A, 00h ; prepare to send data to HC12
mov [recvpc_data_ok], A
mov [HC12_data_counter], A
mov A, [HAPI_STAT_reg] ; sets URDRF bit
or A, HAPI_URDRF
mov [HAPI_STAT_reg], A
loop:
jmp main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -