Chinaunix首页 | 论坛 | 博客
  • 博客访问: 942705
  • 博文数量: 116
  • 博客积分: 3923
  • 博客等级: 中校
  • 技术积分: 1337
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-23 01:22
文章分类

全部博文(116)

文章存档

2013年(1)

2012年(17)

2011年(69)

2009年(29)

分类: LINUX

2012-06-16 11:38:23

好久没更新blog,今天做网卡驱动的时候需要这样一个DMA描述符程序,要求描述符数组8字节对齐,于是写了一个来玩,如下:


点击(此处)折叠或打开

  1. #include <string.h>

  2. #define u32 unsigned int
  3. #define u8 unsigned char

  4. typedef struct bd {
  5.     u32 fsize;
  6.     u8 *pdata;
  7. }bd_t;

  8. #define DMA_TXBD_MAX 3
  9. #define DMA_RXBD_MAX 16

  10. struct txbd {
  11.     bd_t bds[DMA_TXBD_MAX];
  12. }
  13. #ifdef WIN32
  14. #pragma warning(disable:4103)
  15. #pragma pack(8)
  16. #else
  17. __attribute__ ((aligned (8)))
  18. #endif
  19. ;

  20. struct rxbd{
  21.     bd_t bds[DMA_RXBD_MAX];
  22. }
  23. #ifdef WIN32
  24. #pragma warning(disable:4103)
  25. #pragma pack(8)
  26. #else
  27. __attribute__ ((aligned (8)))
  28. #endif
  29. ;

  30. #define DMA_TXBUF_SZ 1536
  31. #define DMA_RXBUF_SZ 1536

  32. static txbd tbds;
  33. static rxbd rbds;

  34. static u8 dma_txbuf[DMA_TXBD_MAX][DMA_TXBUF_SZ];
  35. static u8 dma_rxbuf[DMA_RXBD_MAX][DMA_RXBUF_SZ];

  36. #define init_bd(bdx,bufs,max) do{\
  37.     int x=0; \
  38.     while(x<(max)) { \
  39.     ((bd_t*)(&bdx))[x].fsize=0; \
  40.     ((bd_t*)(&bdx))[x].pdata=(bufs)[x]; \
  41.     x++; \
  42.     } \
  43. }while(0);

  44. #define _dump    printf

  45. #define is_align(addr,bytes) (!((u32)(addr)%(bytes)))
  46. #define check_align(bdx,max,bytes) do { \
  47.     int x=0; \
  48.     while(x<(max)) { \
  49.         if(!is_align(&((bdx).bds[x]),(bytes))) \
  50.             _dump("%02d.align(%d) failed! ptr:0x%08X\r\n",(x),(bytes),&((bdx).bds[x])); \
  51.         else \
  52.             _dump("%02d.align(%d) passed! ptr:0x%08X\r\n",(x),(bytes),&((bdx).bds[x])); \
  53.         x++; \
  54.     } \
  55. }while(0);

  56. #define MSG_VALIDATE "hello world!"

  57. int main(int argc, char* argv[])
  58. {
  59.     int i;
  60.     init_bd(tbds,dma_txbuf,DMA_TXBD_MAX);
  61.     init_bd(rbds,dma_rxbuf,DMA_RXBD_MAX);
  62.     check_align(tbds, DMA_TXBD_MAX, 8);
  63.     check_align(rbds, DMA_RXBD_MAX, 8);

  64.     for(i=0; i<DMA_TXBD_MAX; i++)
  65.     {
  66.         memcpy(tbds.bds[i].pdata, MSG_VALIDATE, sizeof(MSG_VALIDATE));
  67.         _dump("%d. %s\r\n", i, dma_txbuf[i]);
  68.     }

  69.     for(i=0; i<DMA_RXBD_MAX; i++)
  70.     {
  71.         memcpy(rbds.bds[i].pdata, MSG_VALIDATE, sizeof(MSG_VALIDATE));
  72.         _dump("%d. %s\r\n", i, dma_rxbuf[i]);
  73.     }

  74.     return 0;
  75. }

完成!
阅读(4760) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~