ui_memsetcpy.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include <os_common_api.h>
  2. #include <drivers/dma.h>
  3. #include <drivers/cfg_drv/dev_config.h>
  4. #include <display/ui_memsetcpy.h>
  5. static const struct device * dma_dev;
  6. static int dma_chan;
  7. static struct dma_config dma_cfg;
  8. static struct dma_block_config dma_block_cfg;
  9. static K_SEM_DEFINE(dma_sem, 0, 1);
  10. static void dma_complete_callback(const struct device *dev, void *user_data,
  11. uint32_t channel, int status)
  12. {
  13. k_sem_give(&dma_sem);
  14. }
  15. static int dma_transfer_init(void)
  16. {
  17. if (dma_dev) {
  18. return 0;
  19. }
  20. dma_dev = device_get_binding(CONFIG_DMA_0_NAME);
  21. if (dma_dev == NULL) {
  22. SYS_LOG_ERR(CONFIG_DMA_0_NAME " not found");
  23. return -ENODEV;
  24. }
  25. dma_chan = dma_request(dma_dev, 0xFF);
  26. if (dma_chan < 0) {
  27. SYS_LOG_ERR("dma_request failed");
  28. dma_dev = NULL;
  29. return -ENODEV;
  30. }
  31. dma_cfg.complete_callback_en = 1;
  32. dma_cfg.dma_callback = dma_complete_callback;
  33. dma_cfg.dma_slot = 0;
  34. dma_cfg.dest_data_size = 4;
  35. dma_cfg.block_count = 1;
  36. dma_cfg.head_block = &dma_block_cfg;
  37. return 0;
  38. }
  39. static int _memset32(void * buf, uint32_t c32, size_t n)
  40. {
  41. static uint32_t dma_c32 __in_section_unique(ram.noinit);
  42. if (ui_memsetcpy_wait_finish(5000)) {
  43. k_sem_reset(&dma_sem);
  44. }
  45. dma_c32 = c32;
  46. dma_cfg.channel_direction = PERIPHERAL_TO_MEMORY;
  47. dma_block_cfg.block_size = n;
  48. dma_block_cfg.source_address = (uint32_t)&dma_c32;
  49. dma_block_cfg.dest_address = (uint32_t)buf;
  50. if (dma_config(dma_dev, dma_chan, &dma_cfg)) {
  51. SYS_LOG_ERR("dma%d config error\n", dma_chan);
  52. dma_block_cfg.block_size = 0;
  53. return -1;
  54. }
  55. if (dma_start(dma_dev, dma_chan)) {
  56. SYS_LOG_ERR("dma%d start error\n", dma_chan);
  57. dma_block_cfg.block_size = 0;
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. void ui_memset(void * buf, uint8_t c, size_t n)
  63. {
  64. if (((uintptr_t)buf & 0x3) || (n & 0x3)) {
  65. memset(buf, c, n);
  66. return;
  67. }
  68. if (dma_transfer_init()) {
  69. memset(buf, c, n);
  70. return;
  71. }
  72. uint32_t c32 = c | ((uint32_t)c << 8) | ((uint32_t)c << 16) | ((uint32_t)c << 24);
  73. if (_memset32(buf, c32, n)) {
  74. memset(buf, c, n);
  75. }
  76. }
  77. int ui_memset16(void * buf, uint16_t c16, size_t n16)
  78. {
  79. if (((uintptr_t)buf & 0x3) || (n16 & 0x1)) {
  80. return -EINVAL;
  81. }
  82. if (dma_transfer_init()) {
  83. return -EINVAL;
  84. }
  85. uint32_t c32 = c16 | ((uint32_t)c16 << 16);
  86. return _memset32(buf, c32, n16 * 2);
  87. }
  88. int ui_memset32(void * buf, uint32_t c32, size_t n32)
  89. {
  90. if ((uintptr_t)buf & 0x3) {
  91. return -EINVAL;
  92. }
  93. if (dma_transfer_init()) {
  94. return -EINVAL;
  95. }
  96. return _memset32(buf, c32, n32 * 4);
  97. }
  98. void ui_memcpy(void * dest, const void * src, size_t n)
  99. {
  100. if (((uintptr_t)dest & 0x3) || ((uintptr_t)src & 0x3) || (n & 0x3)) {
  101. memcpy(dest, src, n);
  102. return;
  103. }
  104. if (dma_transfer_init()) {
  105. memcpy(dest, src, n);
  106. return;
  107. }
  108. if (ui_memsetcpy_wait_finish(5000)) {
  109. k_sem_reset(&dma_sem);
  110. }
  111. dma_cfg.channel_direction = MEMORY_TO_MEMORY;
  112. dma_block_cfg.block_size = n;
  113. dma_block_cfg.source_address = (uint32_t)src;
  114. dma_block_cfg.dest_address = (uint32_t)dest;
  115. if (dma_config(dma_dev, dma_chan, &dma_cfg)) {
  116. SYS_LOG_ERR("dma%d config error\n", dma_chan);
  117. dma_block_cfg.block_size = 0;
  118. memcpy(dest, src, n);
  119. return;
  120. }
  121. if (dma_start(dma_dev, dma_chan)) {
  122. SYS_LOG_ERR("dma%d start error\n", dma_chan);
  123. dma_block_cfg.block_size = 0;
  124. memcpy(dest, src, n);
  125. return;
  126. }
  127. }
  128. int ui_memsetcpy_wait_finish(int timeout_ms)
  129. {
  130. int res = 0;
  131. if (dma_dev == NULL) {
  132. return 0;
  133. }
  134. if (dma_block_cfg.block_size > 0) {
  135. if (timeout_ms < 0) {
  136. res = k_sem_take(&dma_sem, K_FOREVER);
  137. } else {
  138. res = k_sem_take(&dma_sem, K_MSEC(timeout_ms));
  139. }
  140. if (res == 0) {
  141. dma_block_cfg.block_size = 0;
  142. }
  143. }
  144. return res;
  145. }