1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "hal_tim_basic.h"
9 
TIM_BASIC_Init(TIM_BASIC_Type * TIMx,TIM_BASIC_Init_Type * init)10 bool TIM_BASIC_Init(TIM_BASIC_Type * TIMx, TIM_BASIC_Init_Type * init)
11 {
12     TIMx->CR1 = (TIMx->CR1 & ~(TIM_BASIC_CR1_OPM_MASK | TIM_BASIC_CR1_APRE_MASK) )
13               | TIM_BASIC_CR1_OPM(init->PeriodMode)
14               | ((init->EnablePreloadPeriod) ? TIM_BASIC_CR1_APRE_MASK: 0u)
15               ;
16 
17     /* Check StepFreqHz validity. */
18     if ( (init->StepFreqHz == 0u) || (init->StepFreqHz > init->ClockFreqHz) )
19     {
20         return false;
21     }
22     /* Calculate prescaler of the timer.*/
23     TIMx->PSC = init->ClockFreqHz / init->StepFreqHz - 1u;
24     /* Set the period length. */
25     TIMx->ARR = init->Period;
26     return true;
27 }
28 
TIM_BASIC_Start(TIM_BASIC_Type * TIMx)29 void TIM_BASIC_Start(TIM_BASIC_Type * TIMx)
30 {
31     TIMx->CR1 |= TIM_BASIC_CR1_CEN_MASK;
32 }
33 
TIM_BASIC_Stop(TIM_BASIC_Type * TIMx)34 void TIM_BASIC_Stop(TIM_BASIC_Type * TIMx)
35 {
36     TIMx->CR1 &= ~TIM_BASIC_CR1_CEN_MASK;
37 }
38 
TIM_BASIC_GetCounterValue(TIM_BASIC_Type * TIMx)39 uint32_t TIM_BASIC_GetCounterValue(TIM_BASIC_Type * TIMx)
40 {
41     return TIMx->CNT;
42 }
43 
TIM_BASIC_EnableInterrupts(TIM_BASIC_Type * TIMx,uint32_t interrupts,bool enable)44 void TIM_BASIC_EnableInterrupts(TIM_BASIC_Type *TIMx, uint32_t interrupts, bool enable)
45 {
46     if (enable)
47     {
48         TIMx->DIER |= interrupts;
49     }
50     else
51     {
52         TIMx->DIER &= ~interrupts;
53     }
54 }
55 
TIM_BASIC_EnableDMA(TIM_BASIC_Type * TIMx,uint32_t dmas,bool enable)56 void TIM_BASIC_EnableDMA(TIM_BASIC_Type *TIMx, uint32_t dmas, bool enable)
57 {
58     if (enable)
59     {
60         TIMx->DIER |= dmas;
61     }
62     else
63     {
64         TIMx->DIER &= ~dmas;
65     }
66 }
67 
TIM_BASIC_DoSwTrigger(TIM_BASIC_Type * TIMx,uint32_t swtrgs)68 void TIM_BASIC_DoSwTrigger(TIM_BASIC_Type *TIMx, uint32_t swtrgs)
69 {
70     TIMx->EGR = swtrgs;
71 }
72 
TIM_BASIC_GetInterruptStatus(TIM_BASIC_Type * TIMx)73 uint32_t TIM_BASIC_GetInterruptStatus(TIM_BASIC_Type * TIMx)
74 {
75     return TIMx->SR;
76 }
77 
TIM_BASIC_ClearInterruptStatus(TIM_BASIC_Type * TIMx,uint32_t status)78 void TIM_BASIC_ClearInterruptStatus(TIM_BASIC_Type *TIMx, uint32_t status)
79 {
80     TIMx->SR &= ~status;
81 }
82 
83 /* EOF. */
84 
85