好久没更新blog,今天做网卡驱动的时候需要这样一个DMA描述符程序,要求描述符数组8字节对齐,于是写了一个来玩,如下:
- #include <string.h>
- #define u32 unsigned int
- #define u8 unsigned char
- typedef struct bd {
- u32 fsize;
- u8 *pdata;
- }bd_t;
- #define DMA_TXBD_MAX 3
- #define DMA_RXBD_MAX 16
- struct txbd {
- bd_t bds[DMA_TXBD_MAX];
- }
- #ifdef WIN32
- #pragma warning(disable:4103)
- #pragma pack(8)
- #else
- __attribute__ ((aligned (8)))
- #endif
- ;
- struct rxbd{
- bd_t bds[DMA_RXBD_MAX];
- }
- #ifdef WIN32
- #pragma warning(disable:4103)
- #pragma pack(8)
- #else
- __attribute__ ((aligned (8)))
- #endif
- ;
- #define DMA_TXBUF_SZ 1536
- #define DMA_RXBUF_SZ 1536
- static txbd tbds;
- static rxbd rbds;
- static u8 dma_txbuf[DMA_TXBD_MAX][DMA_TXBUF_SZ];
- static u8 dma_rxbuf[DMA_RXBD_MAX][DMA_RXBUF_SZ];
- #define init_bd(bdx,bufs,max) do{\
- int x=0; \
- while(x<(max)) { \
- ((bd_t*)(&bdx))[x].fsize=0; \
- ((bd_t*)(&bdx))[x].pdata=(bufs)[x]; \
- x++; \
- } \
- }while(0);
- #define _dump printf
- #define is_align(addr,bytes) (!((u32)(addr)%(bytes)))
- #define check_align(bdx,max,bytes) do { \
- int x=0; \
- while(x<(max)) { \
- if(!is_align(&((bdx).bds[x]),(bytes))) \
- _dump("%02d.align(%d) failed! ptr:0x%08X\r\n",(x),(bytes),&((bdx).bds[x])); \
- else \
- _dump("%02d.align(%d) passed! ptr:0x%08X\r\n",(x),(bytes),&((bdx).bds[x])); \
- x++; \
- } \
- }while(0);
- #define MSG_VALIDATE "hello world!"
- int main(int argc, char* argv[])
- {
- int i;
- init_bd(tbds,dma_txbuf,DMA_TXBD_MAX);
- init_bd(rbds,dma_rxbuf,DMA_RXBD_MAX);
- check_align(tbds, DMA_TXBD_MAX, 8);
- check_align(rbds, DMA_RXBD_MAX, 8);
- for(i=0; i<DMA_TXBD_MAX; i++)
- {
- memcpy(tbds.bds[i].pdata, MSG_VALIDATE, sizeof(MSG_VALIDATE));
- _dump("%d. %s\r\n", i, dma_txbuf[i]);
- }
- for(i=0; i<DMA_RXBD_MAX; i++)
- {
- memcpy(rbds.bds[i].pdata, MSG_VALIDATE, sizeof(MSG_VALIDATE));
- _dump("%d. %s\r\n", i, dma_rxbuf[i]);
- }
- return 0;
- }
完成!
阅读(4854) | 评论(0) | 转发(1) |