1 /* 2 * Copyright (c) 2022 HPMicro 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 */ 7 8 #ifndef HPM_DMA_MANAGER_H 9 #define HPM_DMA_MANAGER_H 10 11 #include "hpm_common.h" 12 #include "hpm_dma_drv.h" 13 #include "hpm_soc_feature.h" 14 15 16 #ifdef __cplusplus 17 18 extern "C" { 19 #endif 20 21 22 /** 23 * @brief DMA Manager status codes 24 */ 25 enum { 26 status_dma_manager_no_resource = MAKE_STATUS(status_group_dma_manager, 0), /**< No DMA resource available */ 27 }; 28 29 /** 30 * @brief DMA Channel Interrupt callback 31 * 32 * @param [in] DMA base address 33 * @param [in] channel DMA channel index 34 * @param [in/out] user_data User Data context 35 * @param [in] int_stat DMA interrupt status 36 * bit0 - DMA_CHANNEL_STATUS_ONGOING 37 * bit1 - DMA_CHANNEL_STATUS_ERROR 38 * bit2 - DMA_CHANNEL_STATUS_ABORT 39 * bit3 - DMA_CHANNEL_STATUS_TC 40 */ 41 typedef void (*hpm_dma_channel_callback_t)(DMA_Type *base, uint32_t channel, void *user_data, uint32_t int_stat); 42 43 /** 44 * @brief DMA Resource Structure 45 */ 46 typedef struct _dma_resource { 47 DMA_Type *base; /**< The DMA intance that the allocated channel belongs to */ 48 uint32_t channel; /**< Channel index */ 49 int32_t irq_num; /**< DMA IRQ number */ 50 } hpm_dma_resource_t; 51 52 /** 53 * @brief DMA Channel Context Structure 54 */ 55 typedef struct _dma_channel_context { 56 bool is_allocated; /**< Whether DMA channel was allocated */ 57 void *user_data; /**< User data required by DMA channel callback */ 58 hpm_dma_channel_callback_t callback;/**< DMA channel callback */ 59 } hpm_dma_channel_context_t; 60 61 62 /** 63 * @brief Initialize DMA Manager Context 64 */ 65 void dma_manager_init(void); 66 67 /** 68 * @brief Request DMA resource from DMA Manager 69 * 70 * @param [out] resource DMA resource 71 * @retval status_success if no error occurred 72 * @retval status_invalid_argument if the parameter is invalid 73 * @retval status_dma_manager_no_resource if all DMA channels are occupied; 74 */ 75 hpm_stat_t dma_manager_request_resource(hpm_dma_resource_t *resource); 76 77 /** 78 * @brief Release DMA resource 79 * 80 * @param [in] resource DMA resource 81 * 82 * @retval status_success if no error occurred 83 * @retval status_invalid_argument if the parameter is invalid 84 */ 85 hpm_stat_t dma_manager_release_resource(const hpm_dma_resource_t *resource); 86 87 88 /** 89 * @brief Enable Resource interrupt 90 * @param [in] resource DMA resource 91 * 92 * @retval status_success if no error occurred 93 * @retval status_invalid_argument if any parameters are invalid 94 */ 95 hpm_stat_t dma_manager_enable_channel_interrupt(const hpm_dma_resource_t *resource, uint32_t irq_mask); 96 97 98 /** 99 * @brief Disable Resource interrupt 100 * @param [in] resource DMA resource 101 * 102 * @retval status_success if no error occurred 103 * @retval status_invalid_argument if any parameters are invalid 104 */ 105 hpm_stat_t dma_manager_disable_channel_interrupt(const hpm_dma_resource_t *resource, uint32_t irq_mask); 106 107 108 /** 109 * @brief Enable DMa interrupt 110 * @param [in] resource DMA resource 111 * @param [in] priority Interrupt Priority 112 * 113 * @retval status_success if no error occurred 114 * @retval status_invalid_argument if any parameters are invalid 115 */ 116 hpm_stat_t dma_manager_enable_dma_interrupt(const hpm_dma_resource_t *resource, uint32_t priority); 117 118 /** 119 * @brief Disable DMA interrupt 120 * NOTE: Each DMA instance consists of several DMA channels, disabling the DMA interrupt 121 * will disable the global DMA interrupt for all DMA channels. Please be aware of the 122 * impact 123 * @param [in] resource DMA resource 124 * 125 * @retval status_success if no error occurred 126 * @retval status_invalid_argument if any parameters are invalid 127 */ 128 hpm_stat_t dma_manager_disable_dma_interrupt(const hpm_dma_resource_t *resource); 129 130 131 /** 132 * @brief Install Interrupt Callback for the DMA resource 133 * 134 * @param [in] resource DMA resource 135 * @param [in] callback Interrupt callback for DMA resource 136 * @param [in] user_data User data used in the callback 137 * 138 * @retval status_success if no error occurred 139 * @retval status_invalid_argument if any parameters are invalid 140 */ 141 hpm_stat_t dma_manager_install_interrupt_callback(const hpm_dma_resource_t *resource, hpm_dma_channel_callback_t callback, void *user_data); 142 143 144 145 #ifdef __cplusplus 146 } 147 #endif 148 149 #endif /* HPM_DMA_MANAGER_H */ 150