drv_dma.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-12-02 bigmagic first version
  9. */
  10. #include "drv_dma.h"
  11. #include "raspi4.h"
  12. #include <rtthread.h>
  13. volatile unsigned int __attribute__((aligned(256))) dma_disc[32];
  14. //https://www.raspberrypi.org/forums/viewtopic.php?f=72&t=10276
  15. static struct rt_semaphore dma_sem;
  16. //DMA 0 1 2 3 4 5 6
  17. typedef struct _dma_ctrl_block
  18. {
  19. unsigned int TI; // Transfer information
  20. unsigned int SOURCE_AD; // source address
  21. unsigned int DEST_AD; // destination address
  22. unsigned int TXFR_LEN; // transfer length
  23. unsigned int STRIDE; // 2D mode stride
  24. struct _dma_ctrl_block *NEXTCONBK; // Next control block address
  25. unsigned int DEBUG;
  26. unsigned int reserved1;
  27. } dma_ctrl_block_t;
  28. //DMA 7 8 9 10
  29. typedef struct _dma_lite_ctrl_block
  30. {
  31. unsigned int TI; // Transfer information
  32. unsigned int SOURCE_AD; // source address
  33. unsigned int DEST_AD; // destination address
  34. unsigned int TXFR_LEN; // transfer length
  35. struct _dma_lite_ctrl_block *NEXTCONBK; // Next control block address
  36. unsigned int DEBUG;
  37. unsigned int reserved1;
  38. unsigned int reserved2;
  39. } dma_lite_ctrl_block_t;
  40. //DMA 11 12 13 14 15
  41. typedef struct _dma4_ctrl_block
  42. {
  43. unsigned int TI; // Transfer information
  44. unsigned int SOURCE_AD0; // source address0
  45. unsigned int SOURCE_AD1; // source address1
  46. unsigned int DEST_AD0; // destination address0
  47. unsigned int DEST_AD1; // destination address1
  48. unsigned int TXFR_LEN; // transfer length
  49. unsigned int STRIDE; // 2D mode stride
  50. struct _dma4_ctrl_block *NEXTCONBK; // Next control block address
  51. } dma4_ctrl_block_t;
  52. static dma_lite_ctrl_block_t *ctr_blocks;
  53. static void dma_irq(int irq, void *param)
  54. {
  55. if (DMA_INT_STATUS_REG & DMA_INT7)
  56. {
  57. DMA_CS(7) = DMA_CS_INT;
  58. rt_sem_release(&dma_sem);
  59. }
  60. }
  61. //dma 7 8 9 10:XLENGTH
  62. rt_err_t dma_memcpy(void *src, void *dst, unsigned int size, unsigned int dch, unsigned int timeout)
  63. {
  64. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, dst, size);
  65. /* Stop DMA, if it was already started */
  66. DMA_CS(dch) = DMA_CS_RESET;
  67. /* Clear DMA status flags */
  68. DMA_CS(dch) = DMA_CS_INT | DMA_CS_END; /* Interrupted flag & Transmission ended flag*/
  69. //cb info
  70. ctr_blocks->TI = DMA_TI_SRC_INC | DMA_TI_DEST_INC | DMA_TI_INTEN;
  71. ctr_blocks->SOURCE_AD = (unsigned int)src;
  72. ctr_blocks->DEST_AD = (unsigned int)dst;
  73. ctr_blocks->TXFR_LEN = size;
  74. ctr_blocks->NEXTCONBK = 0;
  75. ctr_blocks->reserved1 = 0;
  76. ctr_blocks->reserved2 = 0;
  77. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, ctr_blocks, sizeof(dma_lite_ctrl_block_t) * 8);
  78. DMA_CONBLK_AD(dch) = (rt_uint32_t)ctr_blocks;
  79. DMA_CS(dch) = DMA_CS_INT | DMA_CS_END | DMA_CS_ACTIVE;
  80. if(rt_sem_take(&dma_sem, timeout) != RT_EOK)
  81. {
  82. rt_kprintf("dma transfer timeout!\n");
  83. return -RT_ERROR;
  84. }
  85. return RT_EOK;
  86. }
  87. void dma_init(unsigned char dch)
  88. {
  89. rt_sem_init(&dma_sem, "dma_sem", 0, RT_IPC_FLAG_FIFO);
  90. ctr_blocks = (dma_lite_ctrl_block_t *)&dma_disc[0]; //rt_malloc(sizeof(DMA_Lite_Control_Block));
  91. //Make sure DMA channel is enabled by
  92. //writing the corresponding bit in DMA_ENABLE in the DMA register to 1
  93. DMA_ENABLE_REG = (1 << dch);
  94. rt_hw_interrupt_install(IRQ_DMA7_DMA8, dma_irq, RT_NULL, "dma_irq");
  95. rt_hw_interrupt_umask(IRQ_DMA7_DMA8);
  96. }