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
)
50 DMA_TypeDef
*DMA
= LL_EX_DMA_Channel_to_DMA(DMAx_Channely
);
51 const uint32_t Channel
= LL_EX_DMA_Channel_to_Channel(DMAx_Channely
);
53 return LL_DMA_Init(DMA
, Channel
, DMA_InitStruct
);
56 __STATIC_INLINE
uint32_t LL_EX_DMA_DeInit(DMA_Channel_TypeDef
*DMAx_Channely
)
58 DMA_TypeDef
*DMA
= LL_EX_DMA_Channel_to_DMA(DMAx_Channely
);
59 const uint32_t Channel
= LL_EX_DMA_Channel_to_Channel(DMAx_Channely
);
61 return LL_DMA_DeInit(DMA
, Channel
);
64 __STATIC_INLINE
void LL_EX_DMA_EnableResource(DMA_Channel_TypeDef
*DMAx_Channely
)
66 SET_BIT(DMAx_Channely
->CCR
, DMA_CCR_EN
);
69 __STATIC_INLINE
void LL_EX_DMA_DisableResource(DMA_Channel_TypeDef
*DMAx_Channely
)
71 CLEAR_BIT(DMAx_Channely
->CCR
, DMA_CCR_EN
);
74 __STATIC_INLINE
void LL_EX_DMA_EnableIT_TC(DMA_Channel_TypeDef
*DMAx_Channely
)
76 SET_BIT(DMAx_Channely
->CCR
, DMA_CCR_TCIE
);
79 __STATIC_INLINE
void LL_EX_DMA_SetDataLength(DMA_Channel_TypeDef
* DMAx_Channely
, uint32_t NbData
)
81 MODIFY_REG(DMAx_Channely
->CNDTR
, DMA_CNDTR_NDT
, NbData
);
84 __STATIC_INLINE
uint32_t LL_EX_DMA_GetDataLength(DMA_Channel_TypeDef
* DMAx_Channely
)
86 return READ_BIT(DMAx_Channely
->CNDTR
, DMA_CNDTR_NDT
);
89 __STATIC_INLINE
void LL_EX_TIM_EnableIT(TIM_TypeDef
*TIMx
, uint32_t Sources
)
91 SET_BIT(TIMx
->DIER
, Sources
);
94 __STATIC_INLINE
void LL_EX_TIM_DisableIT(TIM_TypeDef
*TIMx
, uint32_t Sources
)
96 CLEAR_BIT(TIMx
->DIER
, Sources
);
99 __STATIC_INLINE
void LL_EX_TIM_CC_EnableNChannel(TIM_TypeDef
*TIMx
, uint32_t Channel
)
101 LL_TIM_CC_EnableChannel(TIMx
, 4 * Channel
);