打印
[应用相关]

谁能帮忙分析usb库文件usb_mem.c

[复制链接]
2611|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cpufan|  楼主 | 2008-7-10 16:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
usb的库文件:usb.mem.c,文档不够详细,就两个函数
读了好久,没明白,没辙了,求教,下面贴一个从memory拷贝到PMA的函数:
/*******************************************************************************
* Function Name  : UserToPMABufferCopy
* Description    : Copy a buffer from user memory area to packet memory area (PMA)
* Input          : - pbUsrBuf: pointer to user memory area.
*                  - wPMABufAddr: address into PMA.
*                  - wNBytes: no. of bytes to be copied.
* Output         : None.
* Return         : None    .
*******************************************************************************/
35 void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes)
36 {
37   u32 n = (wNBytes + 1) >> 1;   /* n = (wNBytes + 1) / 2 */
38   u32 i, temp1, temp2;
39   u16 *pdwVal;
40   pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr);
41   for (i = n; i != 0; i--)
42   {
43     temp1 = (u16) * pbUsrBuf;
44     pbUsrBuf++;
45     temp2 = temp1 | (u16) * pbUsrBuf << 8;
46     *pdwVal++ = temp2;
47     pdwVal++;
48     pbUsrBuf++;
49   }
50 }

问题:
1. 40行为什么要(wPMABufAddr * 2 + PMAAddr)?wPMABufAddr 不就是buffer的地址么?
2. 47行为什么还要 pdwVal++,是为了32位数据对齐么?这样的话会不会导致buffer中有空间浪费?相当于我们使用了32位buffer中的低16位。
沙发
香水城| | 2008-7-10 17:15 | 只看该作者

请参考STM32技术参考手册

在手册中有关USB的章节,详细描述了缓冲区的组织结构。搞清楚缓冲区构造,程序就很容易理解了。

使用特权

评论回复
板凳
cpufan|  楼主 | 2008-7-11 11:46 | 只看该作者

香主,我这样理解对不对

发贴前,看过usb部分的manual,反复看了。。。
现在还是不大懂这两个pdwVal++
按照对程序的理解,buffer编址是不是这样的:

0x08  |___H____|___L____|
0x04  |___H____|___L____|
0x00  |___H____|___L____|

H: 高16bit
L: 低16bit
而buffer实现了低16bit,高16bit空着?这样buffer编址是不连续的。
是不是因为APB总线是32位的,才这么实现?
manual原文:
The size of the Packet Memory is 512
bytes, structured as 256 words by 16 bits.

使用特权

评论回复
地板
香水城| | 2008-7-11 11:58 | 只看该作者

请看STM32技术参考手册的第20.5.3节“Buffer descriptor table”

Although the buffer descriptor table is located inside the packet buffer memory, its entries can be considered as additional registers used to configure the location and size of the packet buffers used to exchange data between the USB macro cell and the STM32F10xxx. Due to the common APB bridge limitation on word addressability, all packet memory locations are accessed by the APB using 32-bit aligned addresses, instead of the actual memory location addresses utilized by the USB peripheral for the USB_BTABLE register and buffer description table locations.
 
请注意上面有下划线和加粗的部分。
相关链接:http://www.st.com/stonline/products/literature/rm/13902.pdf

使用特权

评论回复
5
cpufan|  楼主 | 2008-7-11 12:31 | 只看该作者

这段我也仔细看了啊

那么 actual memory location 到底是怎么样的呢,是我上文的理解吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

8

主题

66

帖子

0

粉丝