esp_dma_utils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "esp_err.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * DMA malloc flags
  16. */
  17. /**
  18. * @brief Memory is in PSRAM
  19. */
  20. #define ESP_DMA_MALLOC_FLAG_PSRAM BIT(0)
  21. /**
  22. * @brief Helper function for malloc a DMA capable memory buffer
  23. *
  24. * @param[in] size Size in bytes, the amount of memory to allocate
  25. * @param[in] flags Flags, see `ESP_DMA_MALLOC_FLAG_x`
  26. * @param[out] out_ptr A pointer to the memory allocated successfully
  27. * @param[out] actual_size Actual size for allocation in bytes, when the size you specified doesn't meet the DMA alignment requirements, this value might be bigger than the size you specified. Set null if you don't care this value.
  28. *
  29. * @return
  30. * - ESP_OK:
  31. * - ESP_ERR_INVALID_ARG: Invalid argument
  32. * - ESP_ERR_NO_MEM: No enough memory for allocation
  33. */
  34. esp_err_t esp_dma_malloc(size_t size, uint32_t flags, void **out_ptr, size_t *actual_size);
  35. /**
  36. * @brief Helper function for calloc a DMA capable memory buffer
  37. *
  38. * @param[in] n Number of continuing chunks of memory to allocate
  39. * @param[in] size Size of one chunk, in bytes
  40. * @param[in] flags Flags, see `ESP_DMA_MALLOC_FLAG_x`
  41. * @param[out] out_ptr A pointer to the memory allocated successfully
  42. * @param[out] actual_size Actual size for allocation in bytes, when the size you specified doesn't meet the cache alignment requirements, this value might be bigger than the size you specified. Set null if you don't care this value.
  43. *
  44. * @return
  45. * - ESP_OK:
  46. * - ESP_ERR_INVALID_ARG: Invalid argument
  47. * - ESP_ERR_NO_MEM: No enough memory for allocation
  48. */
  49. esp_err_t esp_dma_calloc(size_t n, size_t size, uint32_t flags, void **out_ptr, size_t *actual_size);
  50. /**
  51. * @brief DMA buffer location
  52. */
  53. typedef enum {
  54. ESP_DMA_BUF_LOCATION_INTERNAL, ///< DMA buffer is in internal memory
  55. ESP_DMA_BUF_LOCATION_PSRAM, ///< DMA buffer is in PSRAM
  56. } esp_dma_buf_location_t;
  57. /**
  58. * @brief Helper function to check if a buffer meets DMA alignment requirements
  59. *
  60. * @param[in] ptr Pointer to the buffer
  61. * @param[in] size Size of the buffer
  62. * @param[in] location Location of the DMA buffer, see `esp_dma_buf_location_t`
  63. *
  64. * @return
  65. * - True: Buffer is aligned
  66. * - False: Buffer is not aligned, or buffer is not DMA capable
  67. */
  68. bool esp_dma_is_buffer_aligned(const void *ptr, size_t size, esp_dma_buf_location_t location);
  69. #ifdef __cplusplus
  70. }
  71. #endif