1 /* 2 * Copyright (c) 2019 Song Qiang <songqiang1304521@gmail.com> 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef DMA_STM32_H_ 8 #define DMA_STM32_H_ 9 10 #include <soc.h> 11 #include <stm32_ll_dma.h> 12 #include <zephyr/drivers/dma.h> 13 #include <zephyr/drivers/clock_control/stm32_clock_control.h> 14 15 /* Maximum data sent in single transfer (Bytes) */ 16 #define DMA_STM32_MAX_DATA_ITEMS 0xffff 17 18 struct dma_stm32_stream { 19 uint32_t direction; 20 #ifdef CONFIG_DMAMUX_STM32 21 int mux_channel; /* stores the dmamux channel */ 22 #endif /* CONFIG_DMAMUX_STM32 */ 23 bool source_periph; 24 bool hal_override; 25 volatile bool busy; 26 uint32_t src_size; 27 uint32_t dst_size; 28 void *user_data; /* holds the client data */ 29 dma_callback_t dma_callback; 30 bool cyclic; 31 }; 32 33 struct dma_stm32_data { 34 struct dma_context dma_ctx; 35 }; 36 37 struct dma_stm32_config { 38 struct stm32_pclken pclken; 39 void (*config_irq)(const struct device *dev); 40 bool support_m2m; 41 uint32_t base; 42 uint32_t max_streams; 43 #ifdef CONFIG_DMAMUX_STM32 44 uint8_t offset; /* position in the list of dmamux channel list */ 45 #endif 46 struct dma_stm32_stream *streams; 47 }; 48 49 uint32_t dma_stm32_id_to_stream(uint32_t id); 50 #if !defined(CONFIG_DMAMUX_STM32) 51 uint32_t dma_stm32_slot_to_channel(uint32_t id); 52 #endif 53 54 typedef void (*dma_stm32_clear_flag_func)(DMA_TypeDef *DMAx); 55 #if !defined(CONFIG_SOC_SERIES_STM32G0X) && \ 56 !defined(CONFIG_SOC_SERIES_STM32H7X) && \ 57 !defined(CONFIG_SOC_SERIES_STM32U0X) 58 typedef uint32_t (*dma_stm32_check_flag_func)(DMA_TypeDef *DMAx); 59 #else 60 typedef uint32_t (*dma_stm32_check_flag_func)(const DMA_TypeDef *DMAx); 61 #endif 62 63 bool dma_stm32_is_tc_active(DMA_TypeDef *DMAx, uint32_t id); 64 void dma_stm32_clear_tc(DMA_TypeDef *DMAx, uint32_t id); 65 bool dma_stm32_is_ht_active(DMA_TypeDef *DMAx, uint32_t id); 66 void dma_stm32_clear_ht(DMA_TypeDef *DMAx, uint32_t id); 67 bool dma_stm32_is_te_active(DMA_TypeDef *DMAx, uint32_t id); 68 void dma_stm32_clear_te(DMA_TypeDef *DMAx, uint32_t id); 69 70 #ifdef CONFIG_DMA_STM32_V1 71 bool dma_stm32_is_dme_active(DMA_TypeDef *DMAx, uint32_t id); 72 void dma_stm32_clear_dme(DMA_TypeDef *DMAx, uint32_t id); 73 bool dma_stm32_is_fe_active(DMA_TypeDef *DMAx, uint32_t id); 74 void dma_stm32_clear_fe(DMA_TypeDef *DMAx, uint32_t id); 75 #endif 76 77 #ifdef CONFIG_DMA_STM32_V2 78 bool dma_stm32_is_gi_active(DMA_TypeDef *DMAx, uint32_t id); 79 void dma_stm32_clear_gi(DMA_TypeDef *DMAx, uint32_t id); 80 #endif 81 82 bool stm32_dma_is_irq_active(DMA_TypeDef *dma, uint32_t id); 83 bool stm32_dma_is_ht_irq_active(DMA_TypeDef *dma, uint32_t id); 84 bool stm32_dma_is_tc_irq_active(DMA_TypeDef *dma, uint32_t id); 85 86 void stm32_dma_dump_stream_irq(DMA_TypeDef *dma, uint32_t id); 87 void stm32_dma_clear_stream_irq(DMA_TypeDef *dma, uint32_t id); 88 bool stm32_dma_is_irq_happened(DMA_TypeDef *dma, uint32_t id); 89 bool stm32_dma_is_unexpected_irq_happened(DMA_TypeDef *dma, uint32_t id); 90 void stm32_dma_enable_stream(DMA_TypeDef *dma, uint32_t id); 91 bool stm32_dma_is_enabled_stream(DMA_TypeDef *dma, uint32_t id); 92 int stm32_dma_disable_stream(DMA_TypeDef *dma, uint32_t id); 93 94 #if !defined(CONFIG_DMAMUX_STM32) 95 void stm32_dma_config_channel_function(DMA_TypeDef *dma, uint32_t id, 96 uint32_t slot); 97 #endif 98 99 #ifdef CONFIG_DMA_STM32_V1 100 void stm32_dma_disable_fifo_irq(DMA_TypeDef *dma, uint32_t id); 101 bool stm32_dma_check_fifo_mburst(LL_DMA_InitTypeDef *DMAx); 102 uint32_t stm32_dma_get_fifo_threshold(uint16_t fifo_mode_control); 103 uint32_t stm32_dma_get_mburst(struct dma_config *config, bool source_periph); 104 uint32_t stm32_dma_get_pburst(struct dma_config *config, bool source_periph); 105 #endif 106 107 #ifdef CONFIG_DMAMUX_STM32 108 /* dma_stm32_ api functions are exported to the dmamux_stm32 */ 109 #define DMA_STM32_EXPORT_API 110 int dma_stm32_configure(const struct device *dev, uint32_t id, 111 struct dma_config *config); 112 int dma_stm32_reload(const struct device *dev, uint32_t id, 113 uint32_t src, uint32_t dst, size_t size); 114 int dma_stm32_start(const struct device *dev, uint32_t id); 115 int dma_stm32_stop(const struct device *dev, uint32_t id); 116 int dma_stm32_get_status(const struct device *dev, uint32_t id, 117 struct dma_status *stat); 118 #else 119 #define DMA_STM32_EXPORT_API static 120 #endif /* CONFIG_DMAMUX_STM32 */ 121 122 #endif /* DMA_STM32_H_*/ 123