summaryrefslogtreecommitdiff
path: root/bsp/acm32/acm32f4xx-nucleo/libraries/HAL_Driver/Src/HAL_TIMER_EX.c
blob: ce8e65693a4509ec499d38c838a381a3be976dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/***********************************************************************
 * Filename    : hal_lpuart.c
 * Description : lpuart driver source file
 * Author(s)   : xwl  
 * version     : V1.0
 * Modify date : 2021-04-02
 ***********************************************************************/
#include "ACM32Fxx_HAL.h"  


/*********************************************************************************
* Function    : HAL_TIMER_ConfigBreakDeadTime  
* Description : configure deadtime parameter   
* Input       : timer handler, break and deadtime configuration  
* Output      : none  
* Author      : xwl                       
**********************************************************************************/  
void HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim, TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
{
  /* Keep this variable initialized to 0 as it is used to configure BDTR register */
    uint32_t break_deadtime_reg = 0U;

    /* Set the BDTR bits */
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_DTG,  sBreakDeadTimeConfig->DeadTime);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_LOCK, sBreakDeadTimeConfig->LockLevel);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_OSSI, sBreakDeadTimeConfig->OffStateIDLEMode);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_OSSR, sBreakDeadTimeConfig->OffStateRunMode);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKE, sBreakDeadTimeConfig->BreakState);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKP, sBreakDeadTimeConfig->BreakPolarity);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_AOE, sBreakDeadTimeConfig->AutomaticOutput);
    MODIFY_REG(break_deadtime_reg, TIM_BDTR_BKF, (sBreakDeadTimeConfig->BreakFilter << TIM_BDTR_BKF_Pos));
  
    /* Set TIMx_BDTR */
    htim->Instance->BDTR = break_deadtime_reg;  

}
  
/*********************************************************************************
* Function    : HAL_TIMEx_ConfigBreakInput  
* Description : Configures the break input source.  
* Input       :
              htim: timer handler 
              BreakInput: TIM_BREAKINPUT_BRK  
              sBreakInputConfig: Break input source configuration   
* Output      :   
* Author      : xwl                       
**********************************************************************************/
HAL_StatusTypeDef HAL_TIMEx_ConfigBreakInput(TIM_HandleTypeDef *htim, uint32_t BreakInput,
                                             TIMEx_BreakInputConfigTypeDef *sBreakInputConfig)
{
    uint32_t tmporx;
    uint32_t bkin_enable_mask;
    uint32_t bkin_polarity_mask;
    uint32_t bkin_enable_bitpos;
    uint32_t bkin_polarity_bitpos;   

    switch(sBreakInputConfig->Source)
    {
        case TIM_BREAKINPUTSOURCE_BKIN:
        {
            bkin_enable_mask = TIM1_AF1_BKINE;
            bkin_enable_bitpos = TIM1_AF1_BKINE_Pos;
            bkin_polarity_mask = TIM1_AF1_BKINP;
            bkin_polarity_bitpos = TIM1_AF1_BKINP_Pos;
            break;
        }
#ifdef HAL_COMP_MODULE_ENABLED  
        case TIM_BREAKINPUTSOURCE_COMP1:
        {
            bkin_enable_mask = TIM1_AF1_BKCMP1E;
            bkin_enable_bitpos = TIM1_AF1_BKCMP1E_Pos;
            bkin_polarity_mask = TIM1_AF1_BKCMP1P;
            bkin_polarity_bitpos = TIM1_AF1_BKCMP1P_Pos;
            break;
        }
        case TIM_BREAKINPUTSOURCE_COMP2:
        {
            bkin_enable_mask = TIM1_AF1_BKCMP2E;
            bkin_enable_bitpos = TIM1_AF1_BKCMP2E_Pos;
            bkin_polarity_mask = TIM1_AF1_BKCMP2P;
            bkin_polarity_bitpos = TIM1_AF1_BKCMP2P_Pos;
            break;
        }
#endif /* COMP1 && COMP2 */

        default:
        {
            bkin_enable_mask = 0U;
            bkin_enable_bitpos = 0U;
            bkin_polarity_mask = 0U;
            bkin_polarity_bitpos = 0U;
            break;
        }
    }
  
    switch(BreakInput)
    {
        case TIM_BREAKINPUT_BRK:
        {
            /* Get the TIMx_AF1 register value */
            tmporx = htim->Instance->AF1;
        
            /* Enable the break input */
            tmporx &= ~bkin_enable_mask;
            tmporx |= (sBreakInputConfig->Enable << bkin_enable_bitpos) & bkin_enable_mask;
        
            /* Set the break input polarity */
            tmporx &= ~bkin_polarity_mask;
            tmporx |= (sBreakInputConfig->Polarity << bkin_polarity_bitpos) & bkin_polarity_mask;
        
            /* Set TIMx_AF1 */
            htim->Instance->AF1 = tmporx;        
            break;
    }

    default:
      break;
    }

    return HAL_OK;  
}   


/*********************************************************************************
* Function    : HAL_TIMEx_ConfigCommutEvent  
* Description : Configure the TIM commutation event sequence. This function is mandatory to use the commutation event 
                in order to update the configuration at each commutation detection on the TRGI input of the Timer.
* Input       :
              htim: timer handler 
              InputTrigger: TIM_TRIGGER_SOURCE_ITR0, TIM_TRIGGER_SOURCE_ITR1...   
              CommutationSource: TIM_COMMUTATION_TRGI, TIM_COMMUTATION_SOFTWARE  
* Output      :   
* Author      : xwl                       
**********************************************************************************/ 
void HAL_TIMEx_ConfigCommutEvent(TIM_HandleTypeDef *htim, uint32_t  InputTrigger, uint32_t  CommutationSource)
{

    if ((InputTrigger == TIM_TRIGGER_SOURCE_ITR0) || (InputTrigger == TIM_TRIGGER_SOURCE_ITR1) ||
      (InputTrigger == TIM_TRIGGER_SOURCE_ITR2) || (InputTrigger == TIM_TRIGGER_SOURCE_ITR3))
    {
        /* Select the Input trigger */
        htim->Instance->SMCR &= (~(BIT4|BIT5|BIT6));
        htim->Instance->SMCR |= InputTrigger;
    }

    /* Select the Capture Compare preload feature */
    htim->Instance->CR2 |= TIM_CR2_CCPC;
    /* Select the Commutation event source */
    htim->Instance->CR2 &= ~TIM_CR2_CCUS;
    htim->Instance->CR2 |= CommutationSource;

    /* Disable Commutation Interrupt */
    HAL_TIM_DISABLE_IT(htim, TIM_IT_COM);

  /* Disable Commutation DMA request */
    HAL_TIM_DISABLE_DMA(htim, TIM_DMA_COM);

}