|
@@ -1,94 +1,73 @@
|
1
|
|
-// TOUT A FAIRE !! //
|
2
|
|
-
|
3
|
|
-/*
|
4
|
|
- indispensable pour pouvoir adresser les registres des périphériques.
|
5
|
|
- Rem : OBLIGATION d'utiliser les définitions utiles contenues dans ce fichier (ex : TIM_CR1_CEN, RCC_APB1ENR_TIM2EN ...)
|
6
|
|
- pour une meilleure lisibilité du code.
|
7
|
|
-
|
8
|
|
- Pour les masques, utiliser également les définitions proposée
|
9
|
|
- Rappel : pour mettre à 1 , reg = reg | Mask (ou Mask est le représente le ou les bits à positionner à 1)
|
10
|
|
- pour mettre à 0 , reg = reg&~ Mask (ou Mask est le représente le ou les bits à positionner à 0)
|
11
|
|
-
|
12
|
|
-*/
|
13
|
|
-
|
14
|
1
|
#include "Timer.h"
|
15
|
2
|
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
|
16
|
|
-#include "stm32f1xx_ll_tim.h"
|
|
3
|
+#include "stm32f1xx_ll_tim.h" // Pour les timers
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+/****************************************************************************
|
|
7
|
+ * INTERRUPTIONS
|
|
8
|
+ ***************************************************************************/
|
17
|
9
|
|
18
|
10
|
|
19
|
11
|
// variable pointeur de fonction permettant de mémoriser le callback à appeler depuis
|
20
|
12
|
// le handler d'IT
|
21
|
|
-void (*Ptr_ItFct_TIM1)(void);
|
22
|
|
-void (*Ptr_ItFct_TIM2)(void);
|
23
|
|
-void (*Ptr_ItFct_TIM3)(void);
|
24
|
|
-void (*Ptr_ItFct_TIM4)(void);
|
25
|
|
-
|
|
13
|
+void (*it_callback_TIM1)(void);
|
|
14
|
+void (*it_callback_TIM2)(void);
|
|
15
|
+void (*it_callback_TIM3)(void);
|
|
16
|
+void (*it_callback_TIM4)(void);
|
26
|
17
|
|
27
|
18
|
|
|
19
|
+void TIM1_UP_IRQHandler(void)
|
|
20
|
+{
|
|
21
|
+ // rabaisser le flag d'IT
|
|
22
|
+ LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
23
|
+ (*it_callback_TIM1)();
|
|
24
|
+}
|
28
|
25
|
|
29
|
|
-/**
|
30
|
|
- * @brief Active l'horloge et règle l'ARR et le PSC du timer visé
|
31
|
|
- * @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir MyTimerStart)
|
32
|
|
- * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
33
|
|
- * int Arr : valeur à placer dans ARR
|
34
|
|
- * int Psc : valeur à placer dans PSC
|
35
|
|
- * @retval None
|
36
|
|
- */
|
37
|
|
-void MyTimer_Conf(TIM_TypeDef * Timer,int Arr, int Psc)
|
|
26
|
+void TIM2_IRQHandler(void)
|
38
|
27
|
{
|
39
|
|
- LL_TIM_InitTypeDef My_LL_Tim_Init_Struct;
|
40
|
|
-
|
41
|
|
- // Validation horloge locale
|
42
|
|
- if (Timer==TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
43
|
|
- else if (Timer==TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
44
|
|
- else if (Timer==TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
45
|
|
- else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
46
|
|
-
|
47
|
|
- // chargement structure Arr, Psc, Up Count
|
48
|
|
- My_LL_Tim_Init_Struct.Autoreload=Arr;
|
49
|
|
- My_LL_Tim_Init_Struct.Prescaler=Psc;
|
50
|
|
- My_LL_Tim_Init_Struct.ClockDivision=LL_TIM_CLOCKDIVISION_DIV1;
|
51
|
|
- My_LL_Tim_Init_Struct.CounterMode=LL_TIM_COUNTERMODE_UP;
|
52
|
|
- My_LL_Tim_Init_Struct.RepetitionCounter=0;
|
53
|
|
-
|
54
|
|
- LL_TIM_Init(Timer,&My_LL_Tim_Init_Struct);
|
55
|
|
-
|
|
28
|
+ // rabaisser le flag d'IT
|
|
29
|
+ LL_TIM_ClearFlag_UPDATE(TIM2);
|
|
30
|
+ (*it_callback_TIM2)();
|
|
31
|
+}
|
56
|
32
|
|
57
|
|
- // Blocage IT
|
58
|
|
- LL_TIM_DisableIT_UPDATE(Timer);
|
59
|
|
-
|
60
|
|
-
|
61
|
|
- // Blocage Timer
|
62
|
|
- LL_TIM_DisableCounter(Timer);
|
63
|
|
-
|
|
33
|
+void TIM3_IRQHandler(void)
|
|
34
|
+{
|
|
35
|
+ // rabaisser le flag d'IT
|
|
36
|
+ LL_TIM_ClearFlag_UPDATE(TIM3);
|
|
37
|
+ (*it_callback_TIM3)();
|
|
38
|
+}
|
64
|
39
|
|
65
|
|
-
|
66
|
|
-}
|
|
40
|
+void TIM4_IRQHandler(void)
|
|
41
|
+{
|
|
42
|
+ // rabaisser le flag d'IT
|
|
43
|
+ LL_TIM_ClearFlag_UPDATE(TIM4);
|
|
44
|
+ (*it_callback_TIM4)();
|
|
45
|
+}
|
67
|
46
|
|
68
|
47
|
|
69
|
48
|
/**
|
70
|
|
- * @brief Démarre le timer considéré
|
|
49
|
+ * @brief Autorise les interruptions
|
71
|
50
|
* @note
|
72
|
51
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
73
|
52
|
* @retval None
|
74
|
53
|
*/
|
75
|
|
-void MyTimer_Start(TIM_TypeDef * Timer)
|
|
54
|
+void Timer_IT_enable(TIM_TypeDef * timer)
|
76
|
55
|
{
|
77
|
|
- LL_TIM_EnableCounter(Timer);
|
|
56
|
+ LL_TIM_EnableIT_UPDATE(timer);
|
78
|
57
|
}
|
79
|
58
|
|
|
59
|
+
|
80
|
60
|
/**
|
81
|
|
- * @brief Arrêt le timer considéré
|
|
61
|
+ * @brief Interdit les interruptions
|
82
|
62
|
* @note
|
83
|
63
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
84
|
64
|
* @retval None
|
85
|
65
|
*/
|
86
|
|
-void MyTimer_Stop(TIM_TypeDef * Timer)
|
|
66
|
+void Timer_IT_disable(TIM_TypeDef * timer)
|
87
|
67
|
{
|
88
|
|
- LL_TIM_DisableCounter(Timer);
|
|
68
|
+ LL_TIM_DisableIT_UPDATE(timer);
|
89
|
69
|
}
|
90
|
70
|
|
91
|
|
-
|
92
|
71
|
/**
|
93
|
72
|
* @brief Configure le Timer considéré en interruption sur débordement.
|
94
|
73
|
* @note A ce stade, les interruptions ne sont pas validés (voir MyTimer_IT_Enable )
|
|
@@ -97,86 +76,139 @@ void MyTimer_Stop(TIM_TypeDef * Timer)
|
97
|
76
|
* int Prio : priorité associée à l'interruption
|
98
|
77
|
* @retval None
|
99
|
78
|
*/
|
100
|
|
-void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void),int Prio)
|
|
79
|
+void Timer_IT_conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
|
101
|
80
|
{
|
102
|
81
|
// affectation de la fonction
|
103
|
|
- if (Timer==TIM1) Ptr_ItFct_TIM1=IT_function;
|
104
|
|
- else if (Timer==TIM2) Ptr_ItFct_TIM2=IT_function;
|
105
|
|
- else if (Timer==TIM3) Ptr_ItFct_TIM3=IT_function;
|
106
|
|
- else Ptr_ItFct_TIM4=IT_function;
|
|
82
|
+ if (timer == TIM1) it_callback_TIM1 = it_callback;
|
|
83
|
+ else if (timer == TIM2) it_callback_TIM2 = it_callback;
|
|
84
|
+ else if (timer == TIM3) it_callback_TIM3 = it_callback;
|
|
85
|
+ else it_callback_TIM4 = it_callback;
|
107
|
86
|
|
108
|
87
|
|
109
|
88
|
// Blocage IT (il faudra la débloquer voir fct suivante)
|
110
|
|
- LL_TIM_DisableIT_UPDATE(Timer);
|
|
89
|
+ LL_TIM_DisableIT_UPDATE(timer);
|
111
|
90
|
|
112
|
91
|
// validation du canal NVIC
|
113
|
92
|
IRQn_Type TIM_irq;
|
114
|
93
|
|
115
|
|
- if (Timer==TIM1) TIM_irq=TIM1_UP_IRQn;
|
116
|
|
- else if (Timer==TIM2) TIM_irq=TIM2_IRQn;
|
117
|
|
- else if (Timer==TIM3) TIM_irq=TIM3_IRQn;
|
118
|
|
- else TIM_irq=TIM4_IRQn;
|
|
94
|
+ if (timer == TIM1) TIM_irq = TIM1_UP_IRQn;
|
|
95
|
+ else if (timer == TIM2) TIM_irq = TIM2_IRQn;
|
|
96
|
+ else if (timer == TIM3) TIM_irq = TIM3_IRQn;
|
|
97
|
+ else TIM_irq = TIM4_IRQn;
|
119
|
98
|
|
120
|
|
- NVIC_SetPriority(TIM_irq, Prio);
|
|
99
|
+ NVIC_SetPriority(TIM_irq, prio);
|
121
|
100
|
NVIC_EnableIRQ(TIM_irq);
|
122
|
101
|
|
123
|
102
|
|
124
|
103
|
}
|
125
|
104
|
|
|
105
|
+/****************************************************************************
|
|
106
|
+ * TIMER
|
|
107
|
+ ***************************************************************************/
|
126
|
108
|
|
127
|
109
|
/**
|
128
|
|
- * @brief Autorise les interruptions
|
|
110
|
+ * @brief Démarre le timer considéré et active les interruptions
|
129
|
111
|
* @note
|
130
|
112
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
131
|
113
|
* @retval None
|
132
|
114
|
*/
|
133
|
|
-void MyTimer_IT_Enable(TIM_TypeDef * Timer)
|
|
115
|
+void Timer_start(TIM_TypeDef * timer)
|
134
|
116
|
{
|
135
|
|
- LL_TIM_EnableIT_UPDATE(Timer);
|
|
117
|
+ LL_TIM_EnableCounter(timer);
|
136
|
118
|
}
|
137
|
119
|
|
138
|
|
-
|
139
|
120
|
/**
|
140
|
|
- * @brief Interdit les interruptions
|
|
121
|
+ * @brief Arrêt le timer considéré et désactive les interruptions
|
141
|
122
|
* @note
|
142
|
123
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
143
|
124
|
* @retval None
|
144
|
125
|
*/
|
145
|
|
-void MyTimer_IT_Disable(TIM_TypeDef * Timer)
|
|
126
|
+void Timer_stop(TIM_TypeDef * timer)
|
146
|
127
|
{
|
147
|
|
- LL_TIM_DisableIT_UPDATE(Timer);
|
148
|
|
-}
|
|
128
|
+ LL_TIM_DisableCounter(timer);
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+/**
|
|
132
|
+ * @brief Active l'horloge et règle l'ARR et le PSC du timer visé.
|
|
133
|
+ * @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir Timer_start)
|
|
134
|
+ * @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
135
|
+ * int Arr : valeur à placer dans ARR
|
|
136
|
+ * int Psc : valeur à placer dans PSC
|
|
137
|
+ * @retval None
|
|
138
|
+ */
|
|
139
|
+void Timer_conf(TIM_TypeDef * timer, int arr, int psc)
|
|
140
|
+{
|
|
141
|
+ LL_TIM_InitTypeDef init_struct;
|
|
142
|
+
|
|
143
|
+ // Validation horloge locale
|
|
144
|
+ if (timer == TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
145
|
+ else if (timer == TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
|
146
|
+ else if (timer == TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
|
147
|
+ else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
|
148
|
+
|
|
149
|
+ // chargement structure Arr, Psc, Up Count
|
|
150
|
+ init_struct.Autoreload = arr;
|
|
151
|
+ init_struct.Prescaler = psc;
|
|
152
|
+ init_struct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
153
|
+ init_struct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
154
|
+ init_struct.RepetitionCounter = 0;
|
|
155
|
+
|
|
156
|
+ LL_TIM_Init(timer,&init_struct);
|
|
157
|
+
|
|
158
|
+ // Blocage interruptions
|
|
159
|
+ Timer_IT_disable(timer);
|
|
160
|
+
|
|
161
|
+ // Blocage Timer
|
|
162
|
+ Timer_stop(timer);
|
|
163
|
+}
|
149
|
164
|
|
|
165
|
+/****************************************************************************
|
|
166
|
+ * PWM INPUT
|
|
167
|
+ ***************************************************************************/
|
150
|
168
|
|
151
|
|
-/*
|
152
|
|
-============ LES INTERRUPTIONS =================================
|
153
|
169
|
|
154
|
|
-*/
|
|
170
|
+/****************************************************************************
|
|
171
|
+ * PWM OUTPUT
|
|
172
|
+ ***************************************************************************/
|
155
|
173
|
|
156
|
|
-void TIM1_UP_IRQHandler(void)
|
|
174
|
+int getArrFromFreq(float freq_khz)
|
157
|
175
|
{
|
158
|
|
- // rabaisser le flag d'IT
|
159
|
|
- LL_TIM_ClearFlag_UPDATE(TIM1);
|
160
|
|
- (*Ptr_ItFct_TIM1)();
|
161
|
|
-}
|
|
176
|
+ return (72000 / freq_khz) - 1;
|
|
177
|
+}
|
162
|
178
|
|
163
|
|
-void TIM2_IRQHandler(void)
|
|
179
|
+void PWMo_conf(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
|
164
|
180
|
{
|
165
|
|
- // rabaisser le flag d'IT
|
166
|
|
- LL_TIM_ClearFlag_UPDATE(TIM2);
|
167
|
|
- (*Ptr_ItFct_TIM2)();
|
168
|
|
-}
|
|
181
|
+ const int arr = getArrFromFreq(freq_khz);
|
|
182
|
+ Timer_conf(timer, arr, 0);
|
|
183
|
+ LL_TIM_OC_InitTypeDef init_struct;
|
|
184
|
+ LL_TIM_OC_StructInit(&init_struct);
|
|
185
|
+
|
|
186
|
+ init_struct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
187
|
+ init_struct.OCState = LL_TIM_OCSTATE_ENABLE;
|
|
188
|
+ init_struct.CompareValue= dutyCycle * arr;
|
|
189
|
+
|
|
190
|
+ LL_TIM_OC_Init(timer, channel, &init_struct);
|
|
191
|
+}
|
169
|
192
|
|
170
|
|
-void TIM3_IRQHandler(void)
|
|
193
|
+void PWMo_setDutyCycle(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
|
171
|
194
|
{
|
172
|
|
- // rabaisser le flag d'IT
|
173
|
|
- LL_TIM_ClearFlag_UPDATE(TIM3);
|
174
|
|
- (*Ptr_ItFct_TIM3)();
|
175
|
|
-}
|
|
195
|
+ int compare = dutyCycle * getArrFromFreq(freq_khz);
|
|
196
|
+ if (channel == LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(timer, compare);
|
|
197
|
+ else if (channel == LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(timer, compare);
|
|
198
|
+ else if (channel == LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(timer, compare);
|
|
199
|
+ else LL_TIM_OC_SetCompareCH4(timer, compare);
|
|
200
|
+}
|
176
|
201
|
|
177
|
|
-void TIM4_IRQHandler(void)
|
|
202
|
+/****************************************************************************
|
|
203
|
+ * ENCODER
|
|
204
|
+ ***************************************************************************/
|
|
205
|
+
|
|
206
|
+void Timer_encoder_conf(TIM_TypeDef * timer, int arr, int psc)
|
178
|
207
|
{
|
179
|
|
- // rabaisser le flag d'IT
|
180
|
|
- LL_TIM_ClearFlag_UPDATE(TIM4);
|
181
|
|
- (*Ptr_ItFct_TIM4)();
|
182
|
|
-}
|
|
208
|
+
|
|
209
|
+}
|
|
210
|
+
|
|
211
|
+int Timer_encoder_get(TIM_TypeDef * timer)
|
|
212
|
+{
|
|
213
|
+ return 0;
|
|
214
|
+}
|