2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
23 #include "stm32g4xx.h"
24 #include "common/utils.h"
26 #define DMA_CHANNEL_MASK 0xFFU
28 // As specified in RM0440 Rev 1 "11.6 DMA registers"
29 #define DMA_FIRST_CHANNEL_OFFSET 0x08U
30 #define DMA_CHANNEL_SIZE 0x14U
32 __STATIC_INLINE DMA_TypeDef
*LL_EX_DMA_Channel_to_DMA(DMA_Channel_TypeDef
*DMAx_Channely
)
34 // clear channel address
35 return (DMA_TypeDef
*) (((uint32_t) DMAx_Channely
) & ((uint32_t) ~DMA_CHANNEL_MASK
));
38 __STATIC_INLINE
uint32_t LL_EX_DMA_Channel_to_Channel(DMA_Channel_TypeDef
*DMAx_Channely
)
41 return (((uint32_t) DMAx_Channely
& DMA_CHANNEL_MASK
) - DMA_FIRST_CHANNEL_OFFSET
) / DMA_CHANNEL_SIZE
+ 1;
44 #undef DMA_CHANNEL_MASK
45 #undef DMA_FIRST_CHANNEL_OFFSET
46 #undef DMA_CHANNEL_SIZE
48 __STATIC_INLINE
uint32_t LL_EX_DMA_Init(DMA_Channel_TypeDef
*DMAx_Channely
, LL_DMA_InitTypeDef
*DMA_InitStruct
) {
49 DMA_TypeDef
*DMA
= LL_EX_DMA_Channel_to_DMA(DMAx_Channely
);
50 const uint32_t Channel
= LL_EX_DMA_Channel_to_Channel(DMAx_Channely
);
52 return LL_DMA_Init(DMA
, Channel
, DMA_InitStruct
);
55 __STATIC_INLINE
uint32_t LL_EX_DMA_DeInit(DMA_Channel_TypeDef
*DMAx_Channely
) {
56 DMA_TypeDef
*DMA
= LL_EX_DMA_Channel_to_DMA(DMAx_Channely
);
57 const uint32_t Channel
= LL_EX_DMA_Channel_to_Channel(DMAx_Channely
);
59 return LL_DMA_DeInit(DMA
, Channel
);
62 __STATIC_INLINE
void LL_EX_DMA_EnableResource(DMA_Channel_TypeDef
*DMAx_Channely
)
64 SET_BIT(DMAx_Channely
->CCR
, DMA_CCR_EN
);
67 __STATIC_INLINE
void LL_EX_DMA_DisableResource(DMA_Channel_TypeDef
*DMAx_Channely
)
69 CLEAR_BIT(DMAx_Channely
->CCR
, DMA_CCR_EN
);
72 __STATIC_INLINE
void LL_EX_DMA_EnableIT_TC(DMA_Channel_TypeDef
*DMAx_Channely
)
74 SET_BIT(DMAx_Channely
->CCR
, DMA_CCR_TCIE
);
77 __STATIC_INLINE
void LL_EX_DMA_SetDataLength(DMA_Channel_TypeDef
* DMAx_Channely
, uint32_t NbData
)
79 MODIFY_REG(DMAx_Channely
->CNDTR
, DMA_CNDTR_NDT
, NbData
);
82 __STATIC_INLINE
uint32_t LL_EX_DMA_GetDataLength(DMA_Channel_TypeDef
* DMAx_Channely
)
84 return READ_BIT(DMAx_Channely
->CNDTR
, DMA_CNDTR_NDT
);
87 __STATIC_INLINE
void LL_EX_TIM_EnableIT(TIM_TypeDef
*TIMx
, uint32_t Sources
)
89 SET_BIT(TIMx
->DIER
, Sources
);
92 __STATIC_INLINE
void LL_EX_TIM_DisableIT(TIM_TypeDef
*TIMx
, uint32_t Sources
)
94 CLEAR_BIT(TIMx
->DIER
, Sources
);
97 __STATIC_INLINE
void LL_EX_TIM_CC_EnableNChannel(TIM_TypeDef
*TIMx
, uint32_t Channel
)
99 LL_TIM_CC_EnableChannel(TIMx
, 4 * Channel
);