31
31
/* Private Defines */
32
32
#define PIN_NOT_USED 0xFF
33
33
#define MAX_RELOAD ((1 << 16 ) - 1 ) // Currently even 32b timers are used as 16b to have generic behavior
34
+ #define REGULAR_CHAN_MASK 0x01
35
+ #define COMPLEMENTARY_CHAN_MASK 0x10
34
36
35
37
/* Private Variables */
36
38
timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL };
@@ -73,9 +75,6 @@ HardwareTimer::HardwareTimer(TIM_TypeDef *instance)
73
75
void HardwareTimer::setup (TIM_TypeDef *instance)
74
76
{
75
77
uint32_t index = get_timer_index (instance);
76
- if (index == UNKNOWN_TIMER) {
77
- Error_Handler ();
78
- }
79
78
80
79
// Already initialized?
81
80
if (_timerObj.handle .Instance ) {
@@ -111,9 +110,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance)
111
110
112
111
// Initialize channel mode and complementary
113
112
for (int i = 0 ; i < TIMER_CHANNELS; i++) {
114
- #if defined(TIM_CCER_CC1NE)
115
- isComplementaryChannel[i] = false ;
116
- #endif
113
+ __ChannelsUsed[i] = 0x00 ;
117
114
_ChannelMode[i] = TIMER_DISABLED;
118
115
}
119
116
@@ -174,14 +171,7 @@ void HardwareTimer::pauseChannel(uint32_t channel)
174
171
175
172
int timAssociatedInputChannel;
176
173
int LLChannel = getLLChannel (channel);
177
- if (LLChannel == -1 ) {
178
- Error_Handler ();
179
- }
180
-
181
174
int interrupt = getIT (channel);
182
- if (interrupt == -1 ) {
183
- Error_Handler ();
184
- }
185
175
186
176
// Disable channel and corresponding interrupt
187
177
__HAL_TIM_DISABLE_IT (&(_timerObj.handle ), interrupt);
@@ -190,11 +180,11 @@ void HardwareTimer::pauseChannel(uint32_t channel)
190
180
/* Starting from G4, new Channel state implementation prevents to restart a channel,
191
181
if the channel has not been explicitly be stopped with HAL interface */
192
182
#if defined(TIM_CHANNEL_N_STATE_SET)
193
- if (isComplementaryChannel [channel - 1 ]) {
183
+ if (__ChannelsUsed [channel - 1 ] & COMPLEMENTARY_CHAN_MASK ) {
194
184
TIM_CHANNEL_N_STATE_SET (&(_timerObj.handle ), getChannel (channel), HAL_TIM_CHANNEL_STATE_READY);
195
- } else
185
+ }
196
186
#endif
197
- {
187
+ if (__ChannelsUsed[channel - 1 ] & REGULAR_CHAN_MASK) {
198
188
TIM_CHANNEL_STATE_SET (&(_timerObj.handle ), getChannel (channel), HAL_TIM_CHANNEL_STATE_READY);
199
189
}
200
190
#endif
@@ -234,11 +224,11 @@ void HardwareTimer::resume(void)
234
224
/* *
235
225
* @brief Convert arduino channel into HAL channel
236
226
* @param Arduino channel [1..4]
237
- * @retval HAL channel. return -1 if arduino channel is invalid
227
+ * @retval HAL channel. Error handler called if arduino channel is invalid
238
228
*/
239
229
int HardwareTimer::getChannel (uint32_t channel)
240
230
{
241
- uint32_t return_value;
231
+ int return_value = - 1 ;
242
232
243
233
switch (channel) {
244
234
case 1 :
@@ -254,21 +244,22 @@ int HardwareTimer::getChannel(uint32_t channel)
254
244
return_value = TIM_CHANNEL_4;
255
245
break ;
256
246
default :
257
- return_value = - 1 ;
247
+ Error_Handler () ;
258
248
}
259
249
return return_value;
260
250
}
261
251
262
252
/* *
263
- * @brief Convert arduino channel into LL channel
253
+ * @brief Convert arduino channel into LL channels used (regular and/or complementary)
264
254
* @param Arduino channel [1..4]
265
- * @retval LL channel. return -1 if arduino channel is invalid
255
+ * @retval LL channel. Error handler called if arduino channel is invalid
266
256
*/
267
257
int HardwareTimer::getLLChannel (uint32_t channel)
268
258
{
269
- uint32_t return_value;
259
+ int return_value = 0 ;
260
+
270
261
#if defined(TIM_CCER_CC1NE)
271
- if (isComplementaryChannel [channel - 1 ]) {
262
+ if (__ChannelsUsed [channel - 1 ] & COMPLEMENTARY_CHAN_MASK ) {
272
263
// Complementary channel
273
264
switch (channel) {
274
265
case 1 :
@@ -288,38 +279,41 @@ int HardwareTimer::getLLChannel(uint32_t channel)
288
279
default :
289
280
return_value = -1 ;
290
281
}
291
- } else
282
+ }
292
283
#endif
293
- {
284
+ if ((return_value != - 1 ) && (__ChannelsUsed[channel - 1 ] & REGULAR_CHAN_MASK)) {
294
285
// Regular channel not complementary
295
286
switch (channel) {
296
287
case 1 :
297
- return_value = LL_TIM_CHANNEL_CH1;
288
+ return_value | = LL_TIM_CHANNEL_CH1;
298
289
break ;
299
290
case 2 :
300
- return_value = LL_TIM_CHANNEL_CH2;
291
+ return_value | = LL_TIM_CHANNEL_CH2;
301
292
break ;
302
293
case 3 :
303
- return_value = LL_TIM_CHANNEL_CH3;
294
+ return_value | = LL_TIM_CHANNEL_CH3;
304
295
break ;
305
296
case 4 :
306
- return_value = LL_TIM_CHANNEL_CH4;
297
+ return_value | = LL_TIM_CHANNEL_CH4;
307
298
break ;
308
299
default :
309
300
return_value = -1 ;
310
301
}
311
302
}
303
+ if (return_value == -1 ) {
304
+ Error_Handler ();
305
+ }
312
306
return return_value;
313
307
}
314
308
315
309
/* *
316
310
* @brief Convert arduino channel into HAL Interrupt ID
317
311
* @param Arduino channel [1..4]
318
- * @retval HAL channel. return -1 if arduino channel is invalid
312
+ * @retval HAL channel. Error handler called if arduino channel is invalid
319
313
*/
320
314
int HardwareTimer::getIT (uint32_t channel)
321
315
{
322
- uint32_t return_value;
316
+ int return_value = - 1 ;
323
317
324
318
switch (channel) {
325
319
case 1 :
@@ -335,7 +329,7 @@ int HardwareTimer::getIT(uint32_t channel)
335
329
return_value = TIM_IT_CC4;
336
330
break ;
337
331
default :
338
- return_value = - 1 ;
332
+ Error_Handler () ;
339
333
}
340
334
return return_value;
341
335
}
@@ -377,19 +371,7 @@ void HardwareTimer::resumeChannel(uint32_t channel)
377
371
{
378
372
int timChannel = getChannel (channel);
379
373
int timAssociatedInputChannel;
380
- if (timChannel == -1 ) {
381
- Error_Handler ();
382
- }
383
-
384
374
int interrupt = getIT (channel);
385
- if (interrupt == -1 ) {
386
- Error_Handler ();
387
- }
388
-
389
- int LLChannel = getLLChannel (channel);
390
- if (LLChannel == -1 ) {
391
- Error_Handler ();
392
- }
393
375
394
376
// Clear flag and enable IT
395
377
if (callbacks[channel]) {
@@ -401,11 +383,11 @@ void HardwareTimer::resumeChannel(uint32_t channel)
401
383
case TIMER_OUTPUT_COMPARE_PWM1:
402
384
case TIMER_OUTPUT_COMPARE_PWM2: {
403
385
#if defined(TIM_CCER_CC1NE)
404
- if (isComplementaryChannel [channel - 1 ]) {
386
+ if (__ChannelsUsed [channel - 1 ] & COMPLEMENTARY_CHAN_MASK ) {
405
387
HAL_TIMEx_PWMN_Start (&(_timerObj.handle ), timChannel);
406
- } else
388
+ }
407
389
#endif
408
- {
390
+ if (__ChannelsUsed[channel - 1 ] & REGULAR_CHAN_MASK) {
409
391
HAL_TIM_PWM_Start (&(_timerObj.handle ), timChannel);
410
392
}
411
393
}
@@ -416,11 +398,11 @@ void HardwareTimer::resumeChannel(uint32_t channel)
416
398
case TIMER_OUTPUT_COMPARE_FORCED_ACTIVE:
417
399
case TIMER_OUTPUT_COMPARE_FORCED_INACTIVE: {
418
400
#if defined(TIM_CCER_CC1NE)
419
- if (isComplementaryChannel [channel - 1 ]) {
401
+ if (__ChannelsUsed [channel - 1 ] & COMPLEMENTARY_CHAN_MASK ) {
420
402
HAL_TIMEx_OCN_Start (&(_timerObj.handle ), timChannel);
421
- } else
403
+ }
422
404
#endif
423
- {
405
+ if (__ChannelsUsed[channel - 1 ] & REGULAR_CHAN_MASK) {
424
406
HAL_TIM_OC_Start (&(_timerObj.handle ), timChannel);
425
407
}
426
408
}
@@ -642,10 +624,6 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch
642
624
TIM_OC_InitTypeDef channelOC;
643
625
TIM_IC_InitTypeDef channelIC;
644
626
645
- if (timChannel == -1 ) {
646
- Error_Handler ();
647
- }
648
-
649
627
/* Configure some default values. Maybe overwritten later */
650
628
channelOC.OCMode = TIMER_NOT_USED;
651
629
channelOC.Pulse = __HAL_TIM_GET_COMPARE (&(_timerObj.handle ), timChannel); // keep same value already written in hardware register
@@ -724,13 +702,18 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch
724
702
HAL_TIM_IC_ConfigChannel (&(_timerObj.handle ), &channelIC, timChannel);
725
703
break ;
726
704
case TIMER_INPUT_FREQ_DUTY_MEASUREMENT:
705
+ // Check if regular channel
706
+ if (STM_PIN_INVERTED (pinmap_function (pin, PinMap_TIM))) {
707
+ Error_Handler ();
708
+ }
727
709
// Configure 1st channel
728
710
channelIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
729
711
channelIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
730
712
HAL_TIM_IC_ConfigChannel (&(_timerObj.handle ), &channelIC, timChannel);
731
713
732
714
// Identify and configure 2nd associated channel
733
715
timAssociatedInputChannel = getAssociatedChannel (channel);
716
+ __ChannelsUsed[timAssociatedInputChannel - 1 ] |= REGULAR_CHAN_MASK;
734
717
_ChannelMode[timAssociatedInputChannel - 1 ] = mode;
735
718
channelIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
736
719
channelIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
@@ -759,9 +742,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch
759
742
Error_Handler ();
760
743
}
761
744
762
- #if defined(TIM_CCER_CC1NE)
763
- isComplementaryChannel[channel - 1 ] = STM_PIN_INVERTED (pinmap_function (pin, PinMap_TIM));
764
- #endif
745
+ __ChannelsUsed[channel - 1 ] |= (STM_PIN_INVERTED (pinmap_function (pin, PinMap_TIM))) ? COMPLEMENTARY_CHAN_MASK : REGULAR_CHAN_MASK;
765
746
}
766
747
}
767
748
@@ -818,10 +799,6 @@ void HardwareTimer::setCaptureCompare(uint32_t channel, uint32_t compare, TimerC
818
799
uint32_t Prescalerfactor = LL_TIM_GetPrescaler (_timerObj.handle .Instance ) + 1 ;
819
800
uint32_t CCR_RegisterValue;
820
801
821
- if (timChannel == -1 ) {
822
- Error_Handler ();
823
- }
824
-
825
802
switch (format) {
826
803
case MICROSEC_COMPARE_FORMAT:
827
804
CCR_RegisterValue = ((compare * (getTimerClkFreq () / 1000000 )) / Prescalerfactor);
@@ -885,10 +862,6 @@ uint32_t HardwareTimer::getCaptureCompare(uint32_t channel, TimerCompareFormat_
885
862
uint32_t Prescalerfactor = LL_TIM_GetPrescaler (_timerObj.handle .Instance ) + 1 ;
886
863
uint32_t return_value;
887
864
888
- if (timChannel == -1 ) {
889
- Error_Handler ();
890
- }
891
-
892
865
switch (format) {
893
866
case MICROSEC_COMPARE_FORMAT:
894
867
return_value = (uint32_t )((CCR_RegisterValue * Prescalerfactor * 1000000.0 ) / getTimerClkFreq ());
@@ -1026,9 +999,6 @@ void HardwareTimer::detachInterrupt()
1026
999
void HardwareTimer::attachInterrupt (uint32_t channel, callback_function_t callback)
1027
1000
{
1028
1001
int interrupt = getIT (channel);
1029
- if (interrupt == -1 ) {
1030
- Error_Handler ();
1031
- }
1032
1002
1033
1003
if ((channel == 0 ) || (channel > (TIMER_CHANNELS + 1 ))) {
1034
1004
Error_Handler (); // only channel 1..4 have an interrupt
@@ -1055,9 +1025,6 @@ void HardwareTimer::attachInterrupt(uint32_t channel, callback_function_t callba
1055
1025
void HardwareTimer::detachInterrupt (uint32_t channel)
1056
1026
{
1057
1027
int interrupt = getIT (channel);
1058
- if (interrupt == -1 ) {
1059
- Error_Handler ();
1060
- }
1061
1028
1062
1029
if ((channel == 0 ) || (channel > (TIMER_CHANNELS + 1 ))) {
1063
1030
Error_Handler (); // only channel 1..4 have an interrupt
@@ -1194,14 +1161,6 @@ bool HardwareTimer::isRunningChannel(uint32_t channel)
1194
1161
int interrupt = getIT (channel);
1195
1162
bool ret;
1196
1163
1197
- if (LLChannel == -1 ) {
1198
- Error_Handler ();
1199
- }
1200
-
1201
- if (interrupt == -1 ) {
1202
- Error_Handler ();
1203
- }
1204
-
1205
1164
// channel is running if: timer is running, and either output channel is
1206
1165
// enabled or interrupt is set
1207
1166
ret = LL_TIM_CC_IsEnabledChannel (_timerObj.handle .Instance , LLChannel)
@@ -1361,6 +1320,9 @@ timer_index_t get_timer_index(TIM_TypeDef *instance)
1361
1320
index = TIMER22_INDEX;
1362
1321
}
1363
1322
#endif
1323
+ if (index == UNKNOWN_TIMER) {
1324
+ Error_Handler ();
1325
+ }
1364
1326
return index ;
1365
1327
}
1366
1328
0 commit comments