WXK
2024-09-18 05e2e954bd127de378a9d1dfbb0ed95d725aad63
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**************************************************************************//**
* @file     pan_hal_timer.c
* @version  V0.0.0
* $Revision: 1 $
* $Date:    23/09/10 $
* @brief    Panchip series Timer HAL source file.
* @note
* Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
*****************************************************************************/
 
#include "pan_hal.h"
 
 
 
TIMER_HandleTypeDef TIMER_Handle_Array[3] =
{
    // TIMER0 Configuration
    {
        .TIMERx = TIMER0,  // Assuming TIMER0 is defined somewhere in your code
        .initObj = {0},  // Initialize with default values or specify them
        .interruptObj = {0},  // Initialize with default values or specify them
        .IRQn = TMR0_IRQn,  // Assuming TIMER0_IRQn is defined as an IRQn_Type for TIMER0
        .callback = NULL,  // Using the dummy callBack
    },
    // TIMER1 Configuration
    {
        .TIMERx = TIMER1,  // Assuming TIMER1 is defined somewhere in your code
        .initObj = {0},  // Initialize with default values or specify them
        .interruptObj = {0},  // Initialize with default values or specify them
        .IRQn = TMR1_IRQn,  // Assuming TIMER1_IRQn is defined as an IRQn_Type for TIMER1
        .callback = NULL,  // Using the dummy callBack
    },
    // TIMER2 Configuration
    {
        .TIMERx = TIMER2,  // Assuming TIMER2 is defined somewhere in your code
        .initObj = {0},  // Initialize with default values or specify them
        .interruptObj = {0},  // Initialize with default values or specify them
        .IRQn = TMR2_IRQn,  // Assuming TIMER2_IRQn is defined as an IRQn_Type for TIMER2
        .callback = NULL,  // Using the dummy callBack
    }
};
 
void HAL_TIMER_Init(TIMER_HandleTypeDef *pTimer)
{
    if(pTimer->TIMERx == TIMER0)
    {
        CLK_APB1PeriphClockCmd(CLK_APB1Periph_TMR0, ENABLE);
        CLK_SetTmrClkSrc(TIMER0, CLK_APB1_TMR0SEL_APB1CLK);
    }
    else if(pTimer->TIMERx == TIMER1)
    {
        CLK_APB2PeriphClockCmd(CLK_APB2Periph_TMR1, ENABLE);
        CLK_SetTmrClkSrc(TIMER1, CLK_APB2_TMR1SEL_APB2CLK);
    }
    else if(pTimer->TIMERx == TIMER2)
    {
        CLK_APB2PeriphClockCmd(CLK_APB2Periph_TMR2, ENABLE);
        CLK_SetTmrClkSrc(TIMER2, CLK_APB2_TMR2SEL_APB2CLK);
    }
    
    TIMER_SetCmpValue(pTimer->TIMERx, pTimer->initObj.tmr0CmpSel, pTimer->initObj.cmpValue);
    
    if(pTimer->initObj.mode == TIMER_MODE_BASECNT)
    {
        TIMER_Open(pTimer->TIMERx, pTimer->initObj.cntMode, pTimer->initObj.freq);
    }  
    else if(pTimer->initObj.mode == TIMER_MODE_EVENTCNT)
    {
        // Counter increase on falling edge
        // Set Timer Mode
        TIMER_SetCountingMode(pTimer->TIMERx, pTimer->initObj.cntMode);
        // Set prescale
        TIMER_SetPrescaleValue(pTimer->TIMERx, pTimer->initObj.prescale);
        TIMER_EnableEventCounter(pTimer->TIMERx,pTimer->initObj.evtCntEdge);
    }
    else if(pTimer->initObj.mode == TIMER_MODE_INCAP)
    {
        TIMER_Open(pTimer->TIMERx, pTimer->initObj.cntMode, pTimer->initObj.freq);
        // Set capture source to ext PIN
        TIMER_SetCaptureSource(pTimer->TIMERx, pTimer->initObj.capSrc);
        // Configure Timer capture mode and capture edge
        TIMER_EnableCapture(pTimer->TIMERx, pTimer->initObj.capMode, pTimer->initObj.capEdge);
    }
    else if(pTimer->initObj.mode == TIMER_MODE_WAKEUP)
    {
        // Set Timer Mode to One Shot Mode
        TIMER_SetCountingMode(pTimer->TIMERx, TIMER_ONESHOT_MODE);
        // Set prescale
        TIMER_SetPrescaleValue(pTimer->TIMERx, pTimer->initObj.prescale);
        // Enable wakeup function
        TIMER_EnableWakeup(pTimer->TIMERx);
    }
    
    HAL_TIMER_Init_INT(pTimer);
 
    NVIC_EnableIRQ(pTimer->IRQn);
}
 
void HAL_TIMER_DeInit(TIMER_HandleTypeDef *pTimer)
{
    if (pTimer->initObj.mode == TIMER_MODE_EVENTCNT) 
    {
        // Disable event counter mode
        TIMER_DisableEventCounter(pTimer->TIMERx);
    }
    else if(pTimer->initObj.mode == TIMER_MODE_WAKEUP) 
    {
        // Disable wakeup function
        TIMER_DisableWakeup(pTimer->TIMERx);    
    }
    // Reset Timer counter/prescale and stop counting
    TIMER_Reset(pTimer->TIMERx);
}
 
void HAL_TIMER_Init_INT(TIMER_HandleTypeDef *pTimer)
{
    if (pTimer->initObj.mode == TIMER_MODE_INCAP)
    {
        // Enable Timer capture interrupt
        TIMER_EnableCaptureInt(pTimer->TIMERx);
    }
    else 
    {
        // Enable interrupt
        TIMER_EnableInt(pTimer->TIMERx);
    }
}
 
void HAL_TIMER_DeInit_INT(TIMER_HandleTypeDef *pTimer)
{
    if (pTimer->initObj.mode == TIMER_MODE_INCAP)
    {
        // Disable Timer capture interrupt
        TIMER_DisableCaptureInt(pTimer->TIMERx);
    }
    else
    {
        // Disable interrupt
        TIMER_DisableInt(pTimer->TIMERx);
    }
}
 
void HAL_TIMER_Start(TIMER_HandleTypeDef *pTimer)
{
    TIMER_Start(pTimer->TIMERx);
}
 
void HAL_TIMER_Stop(TIMER_HandleTypeDef *pTimer)
{
    TIMER_Stop(pTimer->TIMERx);
}
 
uint32_t HAL_TIMER_GetRealFreq(TIMER_HandleTypeDef *pTimer)
{
    uint32_t u32Clk = CLK_GetPeripheralFreq(pTimer->TIMERx);
    uint32_t u32Prescale = 0;
    uint32_t u32RealFreq = pTimer->initObj.freq;
 
    if (u32RealFreq > u32Clk)
        u32RealFreq = u32Clk;
    else if (u32RealFreq == 0)
        u32RealFreq = 1;
 
    u32Prescale = u32Clk / u32RealFreq - 1;
 
    if (u32Prescale > 0xFF)
    {
        //Prescale is 8bit in reg
        u32Prescale = 0xFF;
    }
 
    // Calc real frequency and return
    u32RealFreq = u32Clk / (u32Prescale + 1);
 
    return u32RealFreq;    
}
 
static void TIMER_HandleProc(TIMER_HandleTypeDef *pTimer)
{
    if (TIMER_GetIntFlag(pTimer->TIMERx))
    {
        // Clear Timer interrupt flag
        TIMER_ClearIntFlag(pTimer->TIMERx);
        pTimer->callback(TIMER_CB_FLAG_CNT);
 
    }
 
    if (TIMER_GetCaptureIntFlag(pTimer->TIMERx))
    {
        // Clear Timer capture interrupt flag
        TIMER_ClearCaptureIntFlag(pTimer->TIMERx);
        pTimer->callback(TIMER_CB_FLAG_CAP);
    }
 
    if (TIMER_GetWakeupFlag(pTimer->TIMERx))
    {
        // Clear Timer interrupt flag
        TIMER_ClearWakeupFlag(pTimer->TIMERx, TIMER_GetWakeupFlag(pTimer->TIMERx));
        TIMER_ClearTFFlag(pTimer->TIMERx, TIMER_GetTFFlag(pTimer->TIMERx));
        TIMER_ClearIntFlag(pTimer->TIMERx);        
        pTimer->callback(TIMER_CB_FLAG_WK);
    }
}
 
void TMR0_IRQHandler(void)
{
    TIMER_HandleProc(&TIMER_Handle_Array[0]);
}
 
void TMR1_IRQHandler(void)
{
    TIMER_HandleProc(&TIMER_Handle_Array[1]);
}
 
void TMR2_IRQHandler(void)
{
    TIMER_HandleProc(&TIMER_Handle_Array[2]);
}