搜索
bottom↓
回复: 8
打印 上一主题 下一主题

请问工程师:LGT的V-USB是如何移植的

[复制链接]

出0入0汤圆

跳转到指定楼层
1
发表于 2013-1-31 01:09:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我使用了obdev.at的V-USB进行修改。但是有个问题,就是obdev的V-USB中的底层汇编代码,指令速度有差别的我全部都加了NOP。然后IO口读出写入的我也注意到了IO同步的问题。可是现在这个20M的东西下载到MCU里就是不工作。但是12M的却工作的很好。
希望那个移植的工程师出来赐教一下。
我自己移植的代码:
  1. /* Name: usbdrvasm20.inc
  2. * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
  3. * Author: Jeroen Benschop
  4. * Based on usbdrvasm16.inc from Christian Starkjohann
  5. * Creation Date: 2008-03-05
  6. * Tabsize: 4
  7. * Copyright: (c) 2008 by Jeroen Benschop and OBJECTIVE DEVELOPMENT Software GmbH
  8. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  9. * Revision: $Id: usbdrvasm20.inc 740 2009-04-13 18:23:31Z cs $
  10. */

  11. /* Do not link this file! Link usbdrvasm.S instead, which includes the
  12. * appropriate implementation!
  13. */

  14. /*
  15. General Description:
  16. This file is the 20 MHz version of the asssembler part of the USB driver. It
  17. requires a 20 MHz crystal (not a ceramic resonator and not a calibrated RC
  18. oscillator).

  19. See usbdrv.h for a description of the entire driver.

  20. Since almost all of this code is timing critical, don't change unless you
  21. really know what you are doing! Many parts require not only a maximum number
  22. of CPU cycles, but even an exact number of cycles!
  23. */

  24. #define leap2   x3
  25. #ifdef __IAR_SYSTEMS_ASM__
  26. #define nextInst    $+2
  27. #else
  28. #define nextInst    .+0
  29. #endif

  30. ;max stack usage: [ret(2), YL, SREG, YH, bitcnt, shift, x1, x2, x3, x4, cnt] = 12 bytes
  31. ;nominal frequency: 20 MHz -> 13.333333 cycles per bit, 106.666667 cycles per byte
  32. ; Numbers in brackets are clocks counted from center of last sync bit
  33. ; when instruction starts
  34. ;register use in receive loop:
  35. ; shift assembles the byte currently being received
  36. ; x1 holds the D+ and D- line state
  37. ; x2 holds the previous line state
  38. ; x4 (leap)  is used to add a leap cycle once every three bytes received
  39. ; X3 (leap2) is used to add a leap cycle once every three stuff bits received
  40. ; bitcnt is used to determine when a stuff bit is due
  41. ; cnt holds the number of bytes left in the receive buffer

  42. USB_INTR_VECTOR:
  43.         sbrs        bootcot,0 //如果bootcot的0位被设置
  44.         jmp                UNUSED1_vtr        //跳转到真正的int0(bootcot=0)
  45. ;order of registers pushed: YL, SREG YH, [sofError], bitcnt, shift, x1, x2, x3, x4, cnt
  46.     push    YL                  ;[-28] push only what is necessary to sync with edge ASAP
  47.     in      YL, SREG            ;[-26]
  48.     push    YL                  ;[-25]
  49.     push    YH                  ;[-23]
  50. ;----------------------------------------------------------------------------
  51. ; Synchronize with sync pattern:
  52. ;----------------------------------------------------------------------------
  53. ;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K]
  54. ;sync up with J to K edge during sync pattern -- use fastest possible loops
  55. ;The first part waits at most 1 bit long since we must be in sync pattern.
  56. ;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to
  57. ;waitForJ, ensure that this prerequisite is met.
  58. waitForJ:
  59.     inc     YL
  60.     ;sbis    USBIN, USBMINUS
  61.     in                YL,USBIN
  62.     sbrs        YL        ,USBMINUS
  63.     brne    waitForJ        ; just make sure we have ANY timeout
  64. waitForK:
  65. ;The following code results in a sampling window of < 1/4 bit which meets the spec.
  66.     in                YL,         USBIN
  67.     sbrs    YL,         USBMINUS
  68.     rjmp    foundK              ;[-18]
  69.     in                YL,         USBIN
  70.     sbrs    YL,         USBMINUS
  71.     rjmp    foundK
  72.     in                YL,         USBIN
  73.     sbrs    YL,         USBMINUS
  74.     rjmp    foundK
  75.     in                YL,         USBIN
  76.     sbrs    YL,         USBMINUS
  77.     rjmp    foundK
  78.     in                YL,         USBIN
  79.     sbrs    YL,         USBMINUS
  80.     rjmp    foundK
  81.     in                YL,         USBIN
  82.     sbrs    YL,         USBMINUS
  83.     rjmp    foundK
  84.     in                YL,         USBIN
  85.     sbrs    YL,         USBMINUS
  86.     rjmp    foundK
  87.     in                YL,         USBIN
  88.     sbrs    YL,         USBMINUS
  89.     rjmp    foundK
  90.     in                YL,         USBIN
  91.     sbrs    YL,         USBMINUS
  92.     rjmp    foundK
  93. #if USB_COUNT_SOF
  94.     lds     YL, usbSofCount
  95.     inc     YL
  96.     sts     usbSofCount, YL
  97. #endif  /* USB_COUNT_SOF */
  98. #ifdef USB_SOF_HOOK
  99.     USB_SOF_HOOK
  100. #endif
  101.     rjmp    sofError
  102. foundK:                         ;[-16]
  103. ;{3, 5} after falling D- edge, average delay: 4 cycles
  104. ;bit0 should be at 34 for center sampling. Currently at 4 so 30 cylces till bit 0 sample
  105. ;use 1 bit time for setup purposes, then sample again. Numbers in brackets
  106. ;are cycles from center of first sync (double K) bit after the instruction
  107.     push    bitcnt              ;[-16]
  108.         nop                         ;[-15]
  109.     lds     YL, usbInputBufOffset;[-14]
  110.         nop                         ;[-13]
  111.     clr     YH                  ;[-12]
  112.     subi    YL, lo8(-(usbRxBuf));[-11] [rx loop init]
  113.     sbci    YH, hi8(-(usbRxBuf));[-10] [rx loop init]
  114.     push    shift               ;[-9]
  115.         nop                         ;[-8]
  116.     ldi     shift,0x40          ;[-7] set msb to "1" so processing bit7 can be detected
  117.     nop2                        ;[-6]
  118.         nop                         ;[-5]
  119.     ldi     bitcnt, 5           ;[-4] [rx loop init]
  120.     ;sbis    USBIN, USBMINUS     ;[-3] we want two bits K (sample 3 cycles too early)
  121.         in      x1,    USBIN                        ;1 [8]
  122.         sbrs        x1,    USBMINUS                        ;1 [9]
  123.     rjmp    haveTwoBitsK        ;[-2]
  124.     pop     shift               ;[-1] undo the push from before
  125.     pop     bitcnt              ;[1]
  126.     nop                                                        ;[2]
  127.     rjmp    waitForK            ;[3] this was not the end of sync, retry
  128. ; The entire loop from waitForK until rjmp waitForK above must not exceed two
  129. ; bit times (= 27 cycles).

  130. ;----------------------------------------------------------------------------
  131. ; push more registers and initialize values while we sample the first bits:
  132. ;----------------------------------------------------------------------------
  133. haveTwoBitsK:
  134.     push    x1                  ;[0]
  135.     nop                                                        ;[1]
  136.     push    x2                  ;[2]
  137.     nop                                                        ;[3]
  138.     push    x3                  ;[4] (leap2)
  139.     nop                                                        ;[5]
  140.     ldi     leap2, 0x55         ;[6] add leap cycle on 2nd,5th,8th,... stuff bit
  141.     push    x4                  ;[7] == leap
  142.     nop                                                        ;[8]
  143.     ldi     leap, 0x55          ;[9] skip leap cycle on 2nd,5th,8th,... byte received
  144.     push    cnt                 ;[10]
  145.     nop                                                        ;[11]
  146.     ldi     cnt, USB_BUFSIZE    ;[12] [rx loop init]
  147.     ldi     x2, 1<<USBPLUS      ;[13] current line state is K state. D+=="1", D-=="0"
  148. bit0:      
  149.     in      x1, USBIN           ;[0] sample line state
  150.     andi    x1, USBMASK         ;[1] filter only D+ and D- bits
  151.     nop                                                        ;[2]
  152.     rjmp    handleBit           ;[3] make bit0 14 cycles long

  153. ;----------------------------------------------------------------------------
  154. ; Process bit7. However, bit 6 still may need unstuffing.
  155. ;----------------------------------------------------------------------------

  156. b6checkUnstuff:
  157.     dec     bitcnt              ;[9]
  158.     breq    unstuff6            ;[10]
  159. bit7:
  160.     subi    cnt, 1              ;[11] cannot use dec becaus it does not affect the carry flag
  161.     brcs    overflow            ;[12] Too many bytes received. Ignore packet
  162.     in      x1, USBIN           ;[0] sample line state
  163.     andi    x1, USBMASK         ;[1] filter only D+ and D- bits
  164.     cpse    x1, x2              ;[2] when previous line state equals current line state, handle "1"
  165.     rjmp    b7handle0           ;[3] when line state differs, handle "0"
  166.     sec                         ;[4]
  167.     ror     shift               ;[5] shift "1" into the data
  168.     st      y+, shift           ;[6] store the data into the buffer
  169.     ldi     shift, 0x40         ;[7] reset data for receiving the next byte
  170.     subi    leap, 0x55          ;[9] trick to introduce a leap cycle every 3 bytes
  171.     brcc    nextInst            ;[10 or 11] it will fail after 85 bytes. However low speed can only receive 11
  172.     dec     bitcnt              ;[11 or 12]
  173.     brne    bit0                ;[12 or 13]
  174.     ldi     x1, 1               ;[13 or 14] unstuffing bit 7
  175.     in      bitcnt, USBIN       ;[1] sample stuff bit
  176.     rjmp    unstuff             ;[2]

  177. b7handle0:
  178.     mov     x2,x1               ;[5] Set x2 to current line state
  179.     ldi     bitcnt, 6           ;[6]
  180.     lsr     shift               ;[7] shift "0" into the data
  181.     st      y+, shift           ;[8] store data into the buffer
  182.     ldi     shift, 0x40         ;[10] reset data for receiving the next byte
  183.     subi    leap, 0x55          ;[11] trick to introduce a leap cycle every 3 bytes
  184.     brcs    bit0                ;[12] it will fail after 85 bytes. However low speed can only receive 11
  185.     rjmp    bit0                ;[13]



  186. ;----------------------------------------------------------------------------
  187. ; Handle unstuff
  188. ; x1==0xFF indicate unstuffing bit6
  189. ;----------------------------------------------------------------------------

  190. unstuff6:
  191.     ldi     x1,0xFF             ;[12] indicate unstuffing bit 6
  192.     in      bitcnt, USBIN       ;[0]  sample stuff bit
  193.     nop                         ;[1]  fix timing
  194. unstuff:                        ;b0-5  b6   b7
  195.     mov     x2,bitcnt           ;[3]  [2]  [3]  Set x2 to match line state
  196.     subi    leap2, 0x55         ;[4]  [3]  [4]  delay loop
  197.     brcs    nextInst            ;[5]  [4]  [5]  add one cycle every three stuff bits
  198.     sbci    leap2,0             ;[6]  [5]  [6]
  199.     ldi     bitcnt,6            ;[7]  [6]  [7]  reset bit stuff counter
  200.     andi    x2, USBMASK         ;[8]  [7]  [8] only keep D+ and D-
  201.     cpi     x1,0                ;[9]  [8]  [9]
  202.     brmi    bit7                ;[10] [9]  [10] finished unstuffing bit6 When x1<0
  203.     breq    bitloop             ;[11] ---  [11] finished unstuffing bit0-5 when x1=0
  204.     nop                         ;---  ---  [12]
  205.     in      x1, USBIN           ;---  ---  [0] sample line state for bit0
  206.     andi    x1, USBMASK         ;---  ---  [1] filter only D+ and D- bits
  207.     nop                                                        ;--- ---   [2]
  208.     rjmp    handleBit           ;---  ---  [3] make bit0 14 cycles long

  209. ;----------------------------------------------------------------------------
  210. ; Receiver loop (numbers in brackets are cycles within byte after instr)
  211. ;----------------------------------------------------------------------------
  212. bitloop:
  213.     in      x1, USBIN           ;[0] sample line state
  214.     andi    x1, USBMASK         ;[1] filter only D+ and D- bits
  215.     breq    se0                 ;[2] both lines are low so handle se0
  216. handleBit:
  217.     cpse    x1, x2              ;[3] when previous line state equals current line state, handle "1"
  218.     rjmp    handle0             ;[4] when line state differs, handle "0"
  219.     sec                         ;[5]
  220.     ror     shift               ;[6] shift "1" into the data
  221.     brcs    b6checkUnstuff      ;[7] When after shift C is set, next bit is bit7
  222.     nop2                        ;[8]
  223.     dec     bitcnt              ;[10]
  224.     brne    bitloop             ;[11]
  225.     ldi     x1,0                ;[12] indicate unstuff for bit other than bit6 or bit7
  226.     in      bitcnt, USBIN       ;[0] sample stuff bit
  227.     nop                                                        ;[1]
  228.     rjmp    unstuff             ;[2]

  229. handle0:
  230.     mov     x2, x1              ;[6] Set x2 to current line state
  231.     ldi     bitcnt, 6           ;[7] reset unstuff counter.
  232.     lsr     shift               ;[8] shift "0" into the data
  233.     brcs    bit7                ;[9] When after shift C is set, next bit is bit7
  234.     nop2                         ;[10]
  235.     rjmp    bitloop             ;[12]
  236.    
  237. ;----------------------------------------------------------------------------
  238. ; End of receive loop. Now start handling EOP
  239. ;----------------------------------------------------------------------------

  240. macro POP_STANDARD ; 14 cycles
  241.     pop     cnt
  242.     pop     x4
  243.     pop     x3
  244.     pop     x2
  245.     pop     x1
  246.     pop     shift
  247.     pop     bitcnt
  248.     endm
  249. macro POP_RETI     ; 7 cycles
  250.     pop     YH
  251.     pop     YL
  252.     out     SREG, YL
  253.     pop     YL
  254.     endm



  255. #include "asmcommon.inc"

  256. ; USB spec says:
  257. ; idle = J
  258. ; J = (D+ = 0), (D- = 1)
  259. ; K = (D+ = 1), (D- = 0)
  260. ; Spec allows 7.5 bit times from EOP to SOP for replies
  261. ; 7.5 bit times is 100 cycles. This implementation arrives a bit later at se0
  262. ; then specified in the include file but there is plenty of time

  263. bitstuffN:
  264.     eor     x1, x4          ;[8]
  265.     ldi     x2, 0           ;[9]
  266.     nop2                    ;[10]
  267.     out     USBOUT, x1      ;[12] <-- out
  268.     nop                                                ;[0]
  269.     rjmp    didStuffN       ;[1]
  270.    
  271. bitstuff7:
  272.     eor     x1, x4          ;[6]
  273.     ldi     x2, 0           ;[7] Carry is zero due to brcc
  274.     rol     shift           ;[8] compensate for ror shift at branch destination
  275.     nop2                    ;[9]
  276.     nop                                                ;[11]
  277.     rjmp    didStuff7       ;[12]

  278. sendNakAndReti:
  279.     ldi     x3, USBPID_NAK  ;[-18]
  280.     nop                                                ;[-17]
  281.     rjmp    sendX3AndReti   ;[-16]
  282. sendAckAndReti:
  283.     ldi     cnt, USBPID_ACK ;[-17]
  284. sendCntAndReti:
  285.     mov     x3, cnt         ;[-16]
  286. sendX3AndReti:
  287.     ldi     YL, 20          ;[-15] x3==r20 address is 20
  288.     ldi     YH, 0           ;[-14]
  289.     ldi     cnt, 2          ;[-13]
  290. ;   rjmp    usbSendAndReti      fallthrough

  291. ;usbSend:
  292. ;pointer to data in 'Y'
  293. ;number of bytes in 'cnt' -- including sync byte [range 2 ... 12]
  294. ;uses: x1...x4, btcnt, shift, cnt, Y
  295. ;Numbers in brackets are time since first bit of sync pattern is sent
  296. ;We don't match the transfer rate exactly (don't insert leap cycles every third
  297. ;byte) because the spec demands only 1.5% precision anyway.
  298. usbSendAndReti:             ; 12 cycles until SOP
  299.     in      x2, USBDDR      ;[-12]
  300.     ori     x2, USBMASK     ;[-11]
  301.     sbi     USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups)
  302.     nop                                                ;[-9]
  303.     in      x1, USBOUT      ;[-8] port mirror for tx loop
  304.     out     USBDDR, x2      ;[-7] <- acquire bus
  305. ; need not init x2 (bitstuff history) because sync starts with 0
  306.     ldi     x4, USBMASK     ;[-6] exor mask
  307.     ldi     shift, 0x80     ;[-5] sync byte is first byte sent
  308. txByteLoop:
  309.     ldi     bitcnt, 0x49    ;[-4]        [10] binary 01001001
  310. txBitLoop:
  311.     sbrs    shift, 0        ;[-3] [10]   [11]
  312.     eor     x1, x4          ;[-2] [11]   [12]
  313.     out     USBOUT, x1      ;[-1] [12]   [13]   <-- out N
  314.     ror     shift           ;[0]  [13]   [14]
  315.     ror     x2              ;[1]
  316. didStuffN:
  317.     nop2                    ;[2]
  318.     nop                     ;[4]
  319.     cpi     x2, 0xfc        ;[5]
  320.     brcc    bitstuffN       ;[6]
  321.     lsr     bitcnt          ;[7]
  322.     brcc    txBitLoop       ;[8]
  323.     brne    txBitLoop       ;[9]

  324.     sbrs    shift, 0        ;[10]
  325.     eor     x1, x4          ;[11]
  326. didStuff7:
  327.     out     USBOUT, x1      ;[-1] [13] <-- out 7
  328.     ror     shift           ;[0] [14]
  329.     ror     x2              ;[1]
  330.     nop                     ;[2]
  331.     cpi     x2, 0xfc        ;[3]
  332.     brcc    bitstuff7       ;[4]
  333.     ld      shift, y+       ;[5]
  334.     nop                                                ;[6]
  335.     dec     cnt             ;[7]
  336.     brne    txByteLoop      ;[8]
  337. ;make SE0:
  338.     cbr     x1, USBMASK     ;[9] prepare SE0 [spec says EOP may be 25 to 30 cycles]
  339.     lds     x2, usbNewDeviceAddr;[10]
  340.     lsl     x2              ;[12] we compare with left shifted address
  341.     out     USBOUT, x1      ;[13] <-- out SE0 -- from now 2 bits = 22 cycles until bus idle
  342.     subi    YL, 20 + 2      ;[0] Only assign address on data packets, not ACK/NAK in x3
  343.     sbci    YH, 0           ;[1]
  344. ;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm:
  345. ;set address only after data packet was sent, not after handshake
  346.     breq    skipAddrAssign  ;[2]
  347.     sts     usbDeviceAddr, x2; if not skipped: SE0 is one cycle longer
  348. skipAddrAssign:
  349. ;end of usbDeviceAddress transfer
  350.     ldi     x2, 1<<USB_INTR_PENDING_BIT;[4] int0 occurred during TX -- clear pending flag
  351.     USB_STORE_PENDING(x2)   ;[5]
  352.     ori     x1, USBIDLE     ;[6]
  353.     in      x2, USBDDR      ;[7]
  354.     cbr     x2, USBMASK     ;[8] set both pins to input
  355.     mov     x3, x1          ;[9]
  356.     cbr     x3, USBMASK     ;[10] configure no pullup on both pins
  357.     ldi     x4, 5           ;[11]
  358. se0Delay:
  359.     dec     x4              ;[12] [15] [18] [21] [24]
  360.     brne    se0Delay        ;[13] [16] [19] [22] [25]
  361.     out     USBOUT, x1      ;[26] <-- out J (idle) -- end of SE0 (EOP signal)
  362.     out     USBDDR, x2      ;[27] <-- release bus now
  363.     out     USBOUT, x3      ;[28] <-- ensure no pull-up resistors are active
  364.     nop                                                ;[29]
  365.     rjmp    doReturn                ;[30]
复制代码

出0入0汤圆

2
发表于 2013-1-31 10:20:16 | 只看该作者
因为PC的USB1.0是以1.5Mhz为同步时钟的,所以以12Mhz,24Mhz时钟去同步就是整数采样,理论没有误差累计,更稳定。

出0入0汤圆

3
 楼主| 发表于 2013-1-31 10:35:57 | 只看该作者
logicgreen 发表于 2013-1-31 10:20
因为PC的USB1.0是以1.5Mhz为同步时钟的,所以以12Mhz,24Mhz时钟去同步就是整数采样,理论没有误差累计,更 ...

但是事实上V-USB 也有20M,18M的版本啊。况且24M的你们也没移植。我只好用调分频系数的方法

出0入0汤圆

4
发表于 2013-6-3 22:02:54 | 只看该作者
现在的版本不是直接可以修改CPU频率么?为什么要这么麻烦呢?楼主是想要传输速度提升上去么?

出0入0汤圆

5
 楼主| 发表于 2013-6-4 18:31:41 | 只看该作者
zyin123 发表于 2013-6-3 22:02
现在的版本不是直接可以修改CPU频率么?为什么要这么麻烦呢?楼主是想要传输速度提升上去么? ...

你当CPU频率可以随便改吗……那都有驱动程序对应的。
现在就只有12M 16M 12.8M 20M 18M的CPU频率被支持。

出0入0汤圆

6
发表于 2013-6-5 18:59:58 | 只看该作者
xwkm 发表于 2013-6-4 18:31
你当CPU频率可以随便改吗……那都有驱动程序对应的。
现在就只有12M 16M 12.8M 20M 18M的CPU频率被支持。 ...

是的,最大就到20M了

出0入0汤圆

7
 楼主| 发表于 2013-6-8 18:14:07 | 只看该作者
zyin123 发表于 2013-6-5 18:59
是的,最大就到20M了

所以我才要移植36M和22M的。

出0入0汤圆

8
发表于 2013-6-8 20:37:56 | 只看该作者
发表于 2013-1-31 10:20:16 |只看该作者
因为PC的USB1.0是以1.5Mhz为同步时钟的,所以以12Mhz,24Mhz时钟去同步就是整数采样,理论没有误差累计,更稳定

有24M的版本吗?不懂汇编.

出0入0汤圆

9
发表于 2015-1-24 14:09:16 | 只看该作者
可能还是时钟同步方面的问题 不如先查看下波形
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-20 08:49

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表