dma.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * File : dma.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-11-19 Urey the first version
  23. */
  24. #ifndef _DMA_H_
  25. #define _DMA_H_
  26. /*********************************************************************************************************
  27. ** 头文件
  28. *********************************************************************************************************/
  29. #include <stdlib.h>
  30. #include <rtdef.h>
  31. #include <rtthread.h>
  32. #ifdef __cplusplus
  33. extern "C"{
  34. #endif
  35. #define RT_DMA_CHANNEL(n) (n)
  36. #ifndef RT_DMA_MAX_NODES
  37. # define RT_DMA_MAX_NODES 8
  38. #endif
  39. /*********************************************************************************************************
  40. ** DMA 状态定义
  41. *********************************************************************************************************/
  42. #define RT_DMA_STATUS_IDLE 0 /* DMA 处于空闲模式 */
  43. #define RT_DMA_STATUS_BUSY 1 /* DMA 处于正在工作 */
  44. #define RT_DMA_STATUS_ERROR 2 /* DMA 处于错误状态 */
  45. /*********************************************************************************************************
  46. ** DMA 地址方向定义
  47. *********************************************************************************************************/
  48. #define RT_DMA_ADDR_INC 0 /* 地址增长方式 */
  49. #define RT_DMA_ADDR_FIX 1 /* 地址不变 */
  50. #define RT_DMA_ADDR_DEC 2 /* 地址减少方式 */
  51. /*********************************************************************************************************
  52. ** DMA 传输方向定义
  53. *********************************************************************************************************/
  54. #define RT_DMA_MEM_TO_MEM 0
  55. #define RT_DMA_MEM_TO_DEV 1
  56. #define RT_DMA_DEV_TO_MEM 2
  57. #define RT_DMA_DEV_TO_DEV 3
  58. #define RT_DMA_TRANS_NONE 4
  59. /*********************************************************************************************************
  60. ** DMA 总线宽度
  61. *********************************************************************************************************/
  62. #define RT_DMA_BUSWIDTH_UNDEFINED 0
  63. #define RT_DMA_BUSWIDTH_1_BYTE 1
  64. #define RT_DMA_BUSWIDTH_2_BYTES 2
  65. #define RT_DMA_BUSWIDTH_4_BYTES 4
  66. #define RT_DMA_BUSWIDTH_8_BYTES 8
  67. /*********************************************************************************************************
  68. ** DMA 传输 时间
  69. *********************************************************************************************************/
  70. #define RT_DMA_EVENT_COMPLETE 0x01
  71. #define RT_DMA_EVENT_ERROR 0x02
  72. /*********************************************************************************************************
  73. ** 数据结构
  74. *********************************************************************************************************/
  75. struct rt_dma_channel;
  76. struct dma_message;
  77. struct dma_config;
  78. struct dma_ops
  79. {
  80. void (*reset)(struct rt_dma_channel *dmac);
  81. rt_size_t (*trans)(struct rt_dma_channel *dmac,struct dma_message *message);
  82. int (*status)(struct rt_dma_channel *dmac);
  83. int (*configure)(struct rt_dma_channel *dmac,struct dma_config *config);
  84. };
  85. struct dma_message
  86. {
  87. rt_uint8_t *src_addr; /* 源端缓冲区地址 */
  88. rt_uint8_t *dst_addr; /* 目的端缓冲区地址 */
  89. rt_uint8_t src_option; /* 源端地址方向控制 */
  90. rt_uint8_t dst_option; /* 目的地址方向控制 */
  91. rt_size_t t_size; /* 传输的字节数 */
  92. rt_uint32_t t_mode; /* 传输模式, 自定义 */
  93. void (*complete_cb)(void *data,void *pbuf);
  94. void *complete_arg;
  95. };
  96. struct dma_config
  97. {
  98. rt_uint32_t direction;
  99. rt_uint32_t src_addr_width;
  100. rt_uint32_t dst_addr_width;
  101. rt_uint32_t src_maxburst;
  102. rt_uint32_t dst_maxburst;
  103. };
  104. struct rt_dma_channel
  105. {
  106. int ch;
  107. rt_list_t list; /* channel list */
  108. struct dma_config config;
  109. struct dma_ops *ops;
  110. struct dma_message msg_list[RT_DMA_MAX_NODES];
  111. rt_uint16_t get_index;
  112. rt_uint16_t put_index;
  113. void (*start)(struct rt_dma_channel *dmac,struct dma_message *msg); /* 启动传输 回调函数 */
  114. void (*complete)(struct rt_dma_channel *dmac,struct dma_message *msg); /* 传输完成 回调函数 */
  115. void *user_data; /* 自定义数据 */
  116. };
  117. /*********************************************************************************************************
  118. ** 函数申明
  119. *********************************************************************************************************/
  120. rt_err_t rt_dma_drv_install(struct rt_dma_channel *dmac, struct dma_ops *ops,struct dma_config *config,void* user_data);
  121. struct rt_dma_channel *rt_dma_get_channel(int id);
  122. struct dma_message *rt_dma_get_current_message (struct rt_dma_channel *dmac);
  123. rt_err_t rt_dma_reset (struct rt_dma_channel *dmac);
  124. rt_err_t rt_dma_trans_message (struct rt_dma_channel *dmac,struct dma_message* message);
  125. rt_err_t rt_dma_configture (struct rt_dma_channel *dmac,struct dma_config *config);
  126. rt_err_t rt_dma_contex_service (struct rt_dma_channel *dmac,rt_uint32_t event);
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif /* _DMA_H_ */