📄 firmware.a51
字号:
;-------------------------------------------------------------------------------
; Copyright (c) 2000 by Trenz Electronic.
; Duenner Kirchweg 77, 32257 Buende, Germany, www.trenz-electronic.de
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;-------------------------------------------------------------------------------
; Project: Full-Speed USB 1.1 Function Controller
; File: firmware.a51
; Description: usbSIE reference application, firmware.
; Version: FB, 2000jun21
;-------------------------------------------------------------------------------
$MOD51
STACKTOP EQU 70H ; start of stack (grows up)
ledREG EQU 0F000h ; 7-Segment register address
ledREG0 EQU 00h
ledREG1 EQU 01h
ledREG2 EQU 02h
ledREG3 EQU 03h
ledREG4 EQU 04h
ledREG5 EQU 05h
ledREG6 EQU 06h
ledREG7 EQU 07h
ledREG8 EQU 08h
ledREG9 EQU 09h
ledREGa EQU 0Ah
ledREGb EQU 0Bh
ledREGc EQU 0Ch
ledREGd EQU 0Dh
ledREGe EQU 0Eh
ledREGf EQU 0Fh
;ledREG0 EQU 77h ; "0"
;ledREG1 EQU 12h ; "1"
;ledREG2 EQU 5Eh ; "2"
usbFIFO EQU 0E000h ; usb fifos
usbFIFOlen EQU 8 ; usb fifo depth
usbCTRL EQU 0D000h ; usb endpoint #0 control
usbCTRLread EQU 80h ; control read sequence
usbCTRLwrite EQU 40h ; control write sequence
usbCTRLstatus EQU 20h ; status acknowledge
usbCTRLstall EQU 10h ; STALL endpoint
usbSTAT EQU 0D000h ; usb endpoint #0 status
usbSTATsetup EQU 80h ; setup stage
usbSTATdata EQU 40h ; data stage
usbSTATstatus EQU 20h ; status stage
usbSTATall EQU 0E0h ; any status bit
usbDADX EQU 0C000h ; usb device address
bmRequestType EQU usbFIFO
bRequest EQU usbFIFO + 1
wValueLo EQU usbFIFO + 2
wValueHi EQU usbFIFO + 3
wIndexLo EQU usbFIFO + 4
wIndexHi EQU usbFIFO + 5
wLengthLo EQU usbFIFO + 6
wLengthHi EQU usbFIFO + 7
;-------------------------------------------------------------------------------
DSEG AT 20H
FLAGS: DS 1 ; This register is bit-addressable
Configured EQU FLAGS.0 ; Is this device configured
STALL EQU FLAGS.1 ; Need to STALL endpoint 0
SendData EQU FLAGS.2 ; Need to send data to PC Host
IsDescriptor EQU FLAGS.3 ; Enable a shortcut reply
Temp:
Temp1: DS 1 ; A temporary working register
Temp2: DS 1 ; A temporary working register
Temp3: DS 1 ; A temporary working register
Temp4: DS 1 ; A temporary working register
Temp5: DS 1 ; A temporary working register
Temp6: DS 1 ; A temporary working register
Temp7: DS 1 ; A temporary working register
;Idle_Time: DS 1 ; The time the PC host wants us to wait
ConfigVal: DS 1 ; configuration value
EpHalt: DS 1 ; endpoint HALT feature
DSEG
ORG 30H
;CNTR: DS 1 ; counter for wait routine
;-------------------------------------------------------------------------------
CSEG
ORG 0000H ; program starts at 0 after reset
start:
MOV SP, #STACKTOP ; initialize stack pointer ...
;-- set device address to zero
MOV A, #00h
MOV DPTR, #usbDADX
MOVX @DPTR, A
waitSETUP:
;-- display "0"
MOV A, #ledREG0
MOV DPTR, #ledREG
MOVX @DPTR, A
;-- wait for SETUP transaction
MOV DPTR, #usbSTAT
w1: MOVX A, @DPTR
ANL A, #usbSTATall
CJNE A, #usbSTATsetup, w1
;-- display "1"
MOV A, #ledREG1
MOV DPTR, #ledREG
MOVX @DPTR, A
CALL Dispatch
JMP waitSETUP
;-------------------------------------------------------------------------------
;* * * d i s p a t c h c h a p t e r 9 r e q u e s t s * * * * * * * *
;-------------------------------------------------------------------------------
Dispatch:
;-------------------------------------------
; Reference: USB Serial Bus Specification 1.1
; Chapter 9.3 USB Device Requests
; bmRequestType
; (7) Data transfer direction
; 0= Host-to-device
; 1= Device-to-host
; (6-5) Type
; 00= Standard
; 01= Class
; 10= Vendor
; 11= Reserved
; (4-0) Recipient
; 00000= Device
; 00001= Interface
; 00010= Endpoint
; 00011= Other
; All other codes are reserved
;-------------------------------------------
; Reference: USB Design by Example,
; John Hyde, Wile Computer Publishing,
; ISBN 0-471-37048-7
; Chapter 6 Buttons & Lights, Example 1-Step 3:
; Implement Microcontroller Code
;
; with bmRequest(5) select
; cc= "11" when '1',
; bmRequestType(1 downto 0) when others;
; rr= bRequest(3 downto 0);
; code= cc & rr;
;
; Note: cc recipient
; 00= device
; 01= interface
; 10= endpoint
; 11= class
;- - - - - - - - - - - - - - - - - - - - - -
; rr device interface endpoint class
; 0 devGET_STATUS ifGET_STATUS epGET_STATUS ---
; 1 devCLEAR_FEATURE ifCLEAR_FEATURE epCLEAR_FEATURE xxxGET_REPORT
; 2 --- --- --- xxxGET_IDLE
; 3 devSET_FEATURE ifSET_FEATURE epSET_FEATURE xxxGET_PROTOCOL
; 4 --- --- --- ---
; 5 SET_ADDRESS --- --- ---
; 6 GET_DESCRIPTOR xxxGET_DESCRIPTOR --- ---
; 7 SET_DESCRIPTOR xxxSET_DESCRIPTOR --- ---
; 8 GET_CONFIGURATION --- --- ---
; 9 SET_CONFIGURATION --- --- xxxSET_REPORT
; a --- GET_INTERFACE --- xxxSET_IDLE
; b --- SET_INTERFACE --- xxxSET_PROTOCOL
; c --- --- epSYNC_FRAME
;-------------------------------------------
MOV DPTR, #bmRequestType
MOVX A, @DPTR
MOV C, ACC.7 ; Data transfer direction
MOV SendData, C
ANL A, #01011100b
JNZ BadRequest ; reserved code
MOVX A, @DPTR
MOV C, ACC.0
ANL C, ACC.1
JC BadRequest ; recipient= other
JNB ACC.5, n5 ; bmRequestType(5)
MOV A, #00000011b ; cc= "11"
n5: ANL A, #00000011b ; cc= bmRequestType(1 downto 0)
SWAP A
MOV Temp, A ; Save HI nibble of rr
INC DPTR
MOVX A, @DPTR ; bRequest
ANL A, #00001111b ; bRequest(3 downto 0)
ORL A, Temp ; this is rr!
MOV DPTR, #CommandTable
CALL BumpDPTR ; point to entry
MOVX A, @DPTR ; get the offset
MOV DPTR, #CommandTable
CALL BumpDPTR ; get routine address
PUSH DPL ; create return address on stack
PUSH DPH ; Note: JMP @A+DPTR not used since A, DPTR needed
RET ; go to service routine
BadRequest:
JMP invalid
BumpDPTR: ; Returns (DPTR + ACC)
ADD A, DPL
MOV DPL, A
JNC nc
INC DPH ; Need 16 bit arithmetic here
nc: RET
; Since the table only contains byte offsets, it is important that all these routines are
; within one page (100H) of CommandTable
CommandTable:
;-- device commands
DB devGET_STATUS - CommandTable ; 0
DB devCLEAR_FEATURE - CommandTable ; 1
DB invalid - CommandTable ; 2
DB devSET_FEATURE - CommandTable ; 3
DB invalid - CommandTable ; 4
DB SET_ADDRESS - CommandTable ; 5
DB GET_DESCRIPTOR - CommandTable ; 6
DB SET_DESCRIPTOR - CommandTable ; 7
DB GET_CONFIGURATION - CommandTable ; 8
DB SET_CONFIGURATION - CommandTable ; 9
DB invalid - CommandTable ; 10
DB invalid - CommandTable ; 11
DB invalid - CommandTable ; 12
DB invalid - CommandTable ; 13
DB invalid - CommandTable ; 14
DB invalid - CommandTable ; 15
;-- interface commands
DB ifGET_STATUS - CommandTable ;
DB ifCLEAR_FEATURE - CommandTable ;
DB invalid - CommandTable ;
DB ifSET_FEATURE - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB xxxGET_DESCRIPTOR - CommandTable ;
DB xxxSET_DESCRIPTOR - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB GET_INTERFACE - CommandTable ;
DB SET_INTERFACE - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
;-- endpoint commands
DB epGET_STATUS - CommandTable ;
DB epCLEAR_FEATURE - CommandTable ;
DB invalid - CommandTable ;
DB epSET_FEATURE - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB epSYNCH_FRAME - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
;-- class requests
DB invalid - CommandTable ;
DB xxxGET_REPORT - CommandTable ;
DB xxxGET_IDLE - CommandTable ;
DB xxxGET_PROTOCOL - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB xxxSET_REPORT - CommandTable ;
DB xxxSET_IDLE - CommandTable ;
DB xxxSET_PROTOCOL - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
DB invalid - CommandTable ;
;-------------------------------------------------------------------------------
;* * * s t a n d a r d d e v i c e r e q u e s t s * * * * * * * * * * *
;-------------------------------------------------------------------------------
;*** 9.4.1 Clear Feature *******************************************************
epCLEAR_FEATURE:
MOV DPTR, #wValueLo
MOVX A, @DPTR ; endpoint
;-- todo: store HALT info for every endpoint
MOV EpHalt, #0
JMP NoDataControl
;*** 9.4.2 Get Configuration ***************************************************
GET_CONFIGURATION:
;-- put config val into fifo
MOV DPTR, #usbFIFO
MOV A, ConfigVal
MOVX @DPTR, A
MOV A, #1
JMP ControlRead
;*** 9.4.3 Get Descriptor ******************************************************
GET_DESCRIPTOR:
MOV DPTR, #wValueHI
MOVX A, @DPTR
DEC A
JZ devGET_DESCRIPTOR ; wValueLO= 1
DEC A
JZ cfgGET_DESCRIPTOR ; wValueLO= 2
DEC A
JZ strGET_DESCRIPTOR ; wValueLO= 3
JMP invalid
;*** 9.4.5 Get Status **********************************************************
devGET_STATUS:
MOV A, #1 ; self powered
JMP doGET_STATUS
ifGET_STATUS:
MOV A, #0 ; reserved (zero)
JMP doGET_STATUS
epGET_STATUS:
MOV A, EpHalt ; endpoint HALT
JMP doGET_STATUS
;*** 9.4.6 Set Address *********************************************************
SET_ADDRESS:
MOV DPTR, #wValueLo
MOVX A, @DPTR
;-- set device address (deferred until next SETUP transaction)
MOV DPTR, #usbDADX
MOVX @DPTR, A
JMP NoDataControl
;*** 9.4.7 Set Configuration ***************************************************
SET_CONFIGURATION:
;-- save config value
MOV DPTR, #wValueLo
MOVX A, @DPTR
MOV ConfigVal, A
JMP NoDataControl
;*** 9.4.9 Set Feature *********************************************************
epSET_FEATURE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -