summaryrefslogtreecommitdiff
path: root/ucoo/hal/timer/timer.stm32.tcc
blob: 3ebfcb81fd5c76df58da8aa1dfd0993693927cec (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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#ifndef ucoo_hal_timer_timer_stm32_tcc
#define ucoo_hal_timer_timer_stm32_tcc
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2015 Nicolas Schodet
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// }}}
#include "ucoo/arch/rcc.stm32.hh"
#include "ucoo/arch/reg.hh"

namespace ucoo {

// TODO add more timers.

template<>
struct TimerHardware<TimerInstance::TIM1>
{
    static constexpr TIM_TypeDef *base = reg::TIM1;
    static const Rcc rcc = Rcc::TIM1;
    static int freq_hz () { return rcc_apb2_timer_freq_hz; }
    static const unsigned int max = 0xffff;
    static const bool advanced = true;
    static const bool slave = true;
    static const bool one_pulse = true;
    static const int channels = 4;
};

template<>
struct TimerHardware<TimerInstance::TIM2>
{
    static constexpr TIM_TypeDef *base = reg::TIM2;
    static const Rcc rcc = Rcc::TIM2;
    static int freq_hz () { return rcc_apb1_timer_freq_hz; }
#if defined TARGET_stm32f4
    static const unsigned int max = 0xffffffff;
#else
    static const unsigned int max = 0xffff;
#endif
    static const bool advanced = false;
    static const bool slave = true;
    static const bool one_pulse = true;
    static const int channels = 4;
};

template<>
struct TimerHardware<TimerInstance::TIM3>
{
    static constexpr TIM_TypeDef *base = reg::TIM3;
    static const Rcc rcc = Rcc::TIM3;
    static int freq_hz () { return rcc_apb1_timer_freq_hz; }
    static const unsigned int max = 0xffff;
    static const bool advanced = false;
    static const bool slave = true;
    static const bool one_pulse = true;
    static const int channels = 4;
};

template<>
struct TimerHardware<TimerInstance::TIM4>
{
    static constexpr TIM_TypeDef *base = reg::TIM4;
    static const Rcc rcc = Rcc::TIM4;
    static int freq_hz () { return rcc_apb1_timer_freq_hz; }
    static const unsigned int max = 0xffff;
    static const bool advanced = false;
    static const bool slave = true;
    static const bool one_pulse = true;
    static const int channels = 4;
};

template<>
struct TimerHardware<TimerInstance::TIM5>
{
    static constexpr TIM_TypeDef *base = reg::TIM5;
    static const Rcc rcc = Rcc::TIM5;
    static int freq_hz () { return rcc_apb1_timer_freq_hz; }
#if defined TARGET_stm32f4
    static const unsigned int max = 0xffffffff;
#else
    static const unsigned int max = 0xffff;
#endif
    static const bool advanced = false;
    static const bool slave = true;
    static const bool one_pulse = true;
    static const int channels = 4;
};

template<>
struct TimerHardware<TimerInstance::TIM10>
{
    static constexpr TIM_TypeDef *base = reg::TIM10;
    static const Rcc rcc = Rcc::TIM10;
    static int freq_hz () { return rcc_apb2_timer_freq_hz; }
    static const unsigned int max = 0xffff;
    static const bool advanced = false;
    static const bool slave = false;
    static const bool one_pulse = false;
    static const int channels = 1;
};

template<>
struct TimerHardware<TimerInstance::TIM11>
{
    static constexpr TIM_TypeDef *base = reg::TIM11;
    static const Rcc rcc = Rcc::TIM10;
    static int freq_hz () { return rcc_apb2_timer_freq_hz; }
    static const unsigned int max = 0xffff;
    static const bool advanced = false;
    static const bool slave = false;
    static const bool one_pulse = false;
    static const int channels = 1;
};

template<TimerInstance inst>
const unsigned int TimerHard<inst>::max = TimerHardware<inst>::max;

template<TimerInstance inst>
TimerHard<inst>::~TimerHard ()
{
    disable ();
}

template<TimerInstance inst>
template<typename... Options>
void
TimerHard<inst>::enable (int freq_hz, bool start)
{
    rcc_peripheral_clock_enable (Hard::rcc);
    int in_freq_hz = Hard::freq_hz ();
    int div = in_freq_hz / freq_hz;
    assert (div <= 0x10000 && in_freq_hz / div == freq_hz);
    freq_hz_ = freq_hz;
    using OptionsCombined = CombinedOptions<Options...>;
    Hard::base->CR1 = TIM_CR1_ARPE | OptionsCombined::cr1;
    Hard::base->CR2 = 0;
    if (Hard::slave)
        Hard::base->SMCR = OptionsCombined::smcr;
    if (Hard::channels > 0)
    {
        Hard::base->CCER = 0;
        Hard::base->CCMR1 = OptionsCombined::ccmr1;
        if (Hard::channels > 2)
            Hard::base->CCMR2 = OptionsCombined::ccmr2;
        Hard::base->CCER = OptionsCombined::ccer;
        Hard::base->CCR1 = OptionsCombined::ccr1;
        if (Hard::channels >= 2)
            Hard::base->CCR2 = OptionsCombined::ccr2;
        if (Hard::channels >= 3)
            Hard::base->CCR3 = OptionsCombined::ccr3;
        if (Hard::channels >= 4)
            Hard::base->CCR4 = OptionsCombined::ccr4;
    }
    Hard::base->PSC = div - 1;
    Hard::base->ARR = OptionsCombined::arr ? OptionsCombined::arr : Hard::max;
    if (Hard::advanced)
        Hard::base->BDTR = TIM_BDTR_MOE;
    Hard::base->EGR = TIM_EGR_UG;
    if (start)
        Hard::base->CR1 = TIM_CR1_ARPE | OptionsCombined::cr1 | TIM_CR1_CEN;
}

template<TimerInstance inst>
void
TimerHard<inst>::disable ()
{
    Hard::base->CR1 = 0;
    rcc_peripheral_clock_disable (TimerHardware<inst>::rcc);
    freq_hz_ = 0;
}

template<TimerInstance inst>
void
TimerHard<inst>::start ()
{
    Hard::base->CR1 |= TIM_CR1_CEN;
}

template<TimerInstance inst>
void
TimerHard<inst>::set_reload (unsigned int value)
{
    assert (value <= Hard::max);
    Hard::base->ARR = value;
}

template<TimerInstance inst>
void
TimerHard<inst>::set_output_compare (int ch, unsigned int value)
{
    assert (ch > 0 && ch <= Hard::channels);
    assert (value <= Hard::max);
    *(&Hard::base->CCR1 + ch - 1) = value;
}

template<TimerInstance inst>
unsigned int
TimerHard<inst>::get_value ()
{
    return Hard::base->CNT;
}

template<TimerInstance inst>
unsigned int
TimerHard<inst>::get_input_capture (int ch) const
{
    assert (ch > 0 && ch <= Hard::channels);
    return *(&Hard::base->CCR1 + ch - 1);
}

template<TimerInstance inst>
void
TimerHard<inst>::wait_update () const
{
    unsigned int mask = TIM_SR_UIF;
    while (!(Hard::base->SR & mask))
        ;
    Hard::base->SR = ~mask;
}

template<TimerInstance inst>
void
TimerHard<inst>::wait_input_capture (int ch) const
{
    unsigned int mask = TIM_SR_CC1IF >> 1 << ch;
    while (!(Hard::base->SR & mask))
        ;
    Hard::base->SR = ~mask;
}

template<TimerInstance inst>
void
TimerHard<inst>::enable_interrupt ()
{
    Hard::base->DIER |= TIM_DIER_UIE;
}

template<TimerInstance inst>
void
TimerHard<inst>::disable_interrupt ()
{
    Hard::base->DIER &= ~TIM_DIER_UIE;
}

template<TimerInstance inst>
void
TimerHard<inst>::clear_interrupt ()
{
    Hard::base->SR = ~TIM_SR_UIF;
}

template<TimerInstance inst>
void
TimerHard<inst>::enable_updates ()
{
    Hard::base->CR1 &= ~TIM_CR1_UDIS;
}

template<TimerInstance inst>
void
TimerHard<inst>::disable_updates ()
{
    Hard::base->CR1 |= TIM_CR1_UDIS;
}

template<TimerInstance inst>
TimerHard<inst>::UpdateDisabled::UpdateDisabled (TimerHard<inst> &timer)
    : timer_ (timer)
{
    timer_.disable_updates ();
}

template<TimerInstance inst>
TimerHard<inst>::UpdateDisabled::~UpdateDisabled ()
{
    timer_.enable_updates ();
}

template<TimerInstance inst>
struct TimerHard<inst>::Option
{
    static const TimerInstance opinst = inst;
    static const unsigned cr1 = 0;
    static const unsigned smcr = 0;
    static const unsigned ccmr1 = 0;
    static const unsigned ccmr2 = 0;
    static const unsigned ccer = 0;
    static const unsigned arr = 0;
    static const unsigned ccr1 = 0;
    static const unsigned ccr2 = 0;
    static const unsigned ccr3 = 0;
    static const unsigned ccr4 = 0;
};

template<TimerInstance inst>
template<unsigned int value>
struct TimerHard<inst>::OptionReloadValue : public TimerHard<inst>::Option
{
    static_assert (value <= Hard::max, "value too large");
    static const unsigned arr = value;
};

template<TimerInstance inst>
struct TimerHard<inst>::OptionOnePulse : public TimerHard<inst>::Option
{
    static_assert (Hard::one_pulse, "no one pulse mode");
    static const unsigned cr1 = TIM_CR1_OPM;
};

template<TimerInstance inst>
template<int timer_input>
struct TimerHard<inst>::OptionTrigger : public TimerHard<inst>::Option
{
    static_assert (Hard::slave, "no external trigger");
    static_assert (timer_input >= 1 && timer_input <= 2, "no such input");
    static const unsigned smcr =
        (timer_input == 1 ? TIM_SMCR_TS_TI1FP1 : TIM_SMCR_TS_TI1FP2)
        | TIM_SMCR_SMS_TM;
};

template<TimerInstance inst>
template<int channel, typename TimerHard<inst>::Filter filter,
    typename TimerHard<inst>::Map map,
    typename TimerHard<inst>::Polarity polarity>
struct TimerHard<inst>::OptionInputCapture
{
    static_assert (channel == 1, "no such channel");
    // Always activate input capture (through Polarity values), even when only
    // used as a trigger, no harm is done.
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Filter filter,
    typename TimerHard<inst>::Map map,
    typename TimerHard<inst>::Polarity polarity>
struct TimerHard<inst>::OptionInputCapture<1, filter, map, polarity>
    : public TimerHard<inst>::Option
{
    static_assert (1 <= TimerHard<inst>::Hard::channels, "no such channel");
    static const unsigned ccmr1 =
        (static_cast<int> (filter)
         * (TIM_CCMR1_IC1F & ~(TIM_CCMR1_IC1F - 1)))
        | (static_cast<int> (map)
           * (TIM_CCMR1_CC1S & ~(TIM_CCMR1_CC1S - 1)));
    static const unsigned ccer = static_cast<int> (polarity) * TIM_CCER_CC1E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Filter filter,
    typename TimerHard<inst>::Map map,
    typename TimerHard<inst>::Polarity polarity>
struct TimerHard<inst>::OptionInputCapture<2, filter, map, polarity>
    : public TimerHard<inst>::Option
{
    static_assert (2 <= TimerHard<inst>::Hard::channels, "no such channel");
    static const unsigned ccmr1 =
        (static_cast<int> (filter)
         * (TIM_CCMR1_IC2F & ~(TIM_CCMR1_IC2F - 1)))
        | (static_cast<int> (map)
           * (TIM_CCMR1_CC2S & ~(TIM_CCMR1_CC2S - 1)));
    static const unsigned ccer = static_cast<int> (polarity) * TIM_CCER_CC2E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Filter filter,
    typename TimerHard<inst>::Map map,
    typename TimerHard<inst>::Polarity polarity>
struct TimerHard<inst>::OptionInputCapture<3, filter, map, polarity>
    : public TimerHard<inst>::Option
{
    static_assert (3 <= TimerHard<inst>::Hard::channels, "no such channel");
    static const unsigned ccmr2 =
        (static_cast<int> (filter)
         * (TIM_CCMR2_IC3F & ~(TIM_CCMR2_IC3F - 1)))
        | (static_cast<int> (map)
           * (TIM_CCMR2_CC3S & ~(TIM_CCMR2_CC3S - 1)));
    static const unsigned ccer = static_cast<int> (polarity) * TIM_CCER_CC3E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Filter filter,
    typename TimerHard<inst>::Map map,
    typename TimerHard<inst>::Polarity polarity>
struct TimerHard<inst>::OptionInputCapture<4, filter, map, polarity>
    : public TimerHard<inst>::Option
{
    static_assert (4 <= TimerHard<inst>::Hard::channels, "no such channel");
    static const unsigned ccmr2 =
        (static_cast<int> (filter)
         * (TIM_CCMR2_IC4F & ~(TIM_CCMR2_IC4F - 1)))
        | (static_cast<int> (map)
           * (TIM_CCMR2_CC4S & ~(TIM_CCMR2_CC4S - 1)));
    static const unsigned ccer = static_cast<int> (polarity) * TIM_CCER_CC4E;
};

template<TimerInstance inst>
template<int channel, typename TimerHard<inst>::Polarity polarity,
    typename TimerHard<inst>::Complementary complementary>
struct TimerHard<inst>::OptionOutputCompare
{
    static_assert (channel == 1, "no such channel");
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Polarity polarity,
    typename TimerHard<inst>::Complementary complementary>
struct TimerHard<inst>::OptionOutputCompare<1, polarity, complementary>
    : public TimerHard<inst>::Option
{
    static_assert (1 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (complementary == TimerHard<inst>::Complementary::DO_NOT_USE
                   || TimerHard<inst>::Hard::advanced,
                   "no complementary output");
    static const unsigned ccmr1 = TIM_CCMR1_OC1M_PWM1 | TIM_CCMR1_OC1PE;
    static const unsigned ccer = static_cast<int> (complementary)
        * static_cast<int> (polarity) * TIM_CCER_CC1E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Polarity polarity,
    typename TimerHard<inst>::Complementary complementary>
struct TimerHard<inst>::OptionOutputCompare<2, polarity, complementary>
    : public TimerHard<inst>::Option
{
    static_assert (2 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (complementary == TimerHard<inst>::Complementary::DO_NOT_USE
                   || TimerHard<inst>::Hard::advanced,
                   "no complementary output");
    static const unsigned ccmr1 = TIM_CCMR1_OC2M_PWM1 | TIM_CCMR1_OC2PE;
    static const unsigned ccer = static_cast<int> (complementary)
        * static_cast<int> (polarity) * TIM_CCER_CC2E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Polarity polarity,
    typename TimerHard<inst>::Complementary complementary>
struct TimerHard<inst>::OptionOutputCompare<3, polarity, complementary>
    : public TimerHard<inst>::Option
{
    static_assert (3 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (complementary == TimerHard<inst>::Complementary::DO_NOT_USE
                   || TimerHard<inst>::Hard::advanced,
                   "no complementary output");
    static const unsigned ccmr2 = TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE;
    static const unsigned ccer = static_cast<int> (complementary)
        * static_cast<int> (polarity) * TIM_CCER_CC3E;
};

template<TimerInstance inst>
template<typename TimerHard<inst>::Polarity polarity,
    typename TimerHard<inst>::Complementary complementary>
struct TimerHard<inst>::OptionOutputCompare<4, polarity, complementary>
    : public TimerHard<inst>::Option
{
    static_assert (4 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (complementary == TimerHard<inst>::Complementary::DO_NOT_USE
                   || TimerHard<inst>::Hard::advanced,
                   "no complementary output");
    static const unsigned ccmr2 = TIM_CCMR2_OC4M_PWM1 | TIM_CCMR2_OC4PE;
    static const unsigned ccer = static_cast<int> (complementary)
        * static_cast<int> (polarity) * TIM_CCER_CC4E;
};

template<TimerInstance inst>
template<int channel, unsigned int value>
struct TimerHard<inst>::OptionOutputCompareValue
    : public TimerHard<inst>::Option
{
    static_assert (channel == 1, "no such channel");
};

template<TimerInstance inst>
template<unsigned int value>
struct TimerHard<inst>::OptionOutputCompareValue<1, value>
    : public TimerHard<inst>::Option
{
    static_assert (1 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (value <= TimerHard<inst>::Hard::max, "value too large");
    static const unsigned ccr1 = value;
};

template<TimerInstance inst>
template<unsigned int value>
struct TimerHard<inst>::OptionOutputCompareValue<2, value>
    : public TimerHard<inst>::Option
{
    static_assert (2 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (value <= TimerHard<inst>::Hard::max, "value too large");
    static const unsigned ccr2 = value;
};

template<TimerInstance inst>
template<unsigned int value>
struct TimerHard<inst>::OptionOutputCompareValue<3, value>
    : public TimerHard<inst>::Option
{
    static_assert (3 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (value <= TimerHard<inst>::Hard::max, "value too large");
    static const unsigned ccr3 = value;
};

template<TimerInstance inst>
template<unsigned int value>
struct TimerHard<inst>::OptionOutputCompareValue<4, value>
    : public TimerHard<inst>::Option
{
    static_assert (4 <= TimerHard<inst>::Hard::channels, "no such channel");
    static_assert (value <= TimerHard<inst>::Hard::max, "value too large");
    static const unsigned ccr4 = value;
};

template<TimerInstance inst>
template<typename... Options>
struct TimerHard<inst>::CombinedOptions : public TimerHard<inst>::Option
{
};

template<TimerInstance inst>
template<typename Option, typename... Options>
struct TimerHard<inst>::CombinedOptions<Option, Options...>
{
    static_assert (inst == Option::opinst, "option for another timer");
    static const TimerInstance opinst = inst;
    static const unsigned cr1 = Option::cr1
        | CombinedOptions<Options...>::cr1;
    static const unsigned smcr = Option::smcr
        | CombinedOptions<Options...>::smcr;
    static const unsigned ccmr1 = Option::ccmr1
        | CombinedOptions<Options...>::ccmr1;
    static const unsigned ccmr2 = Option::ccmr2
        | CombinedOptions<Options...>::ccmr2;
    static const unsigned ccer = Option::ccer
        | CombinedOptions<Options...>::ccer;
    static const unsigned arr = Option::arr
        | CombinedOptions<Options...>::arr;
    static_assert (Option::arr == 0 || Option::arr == arr,
                   "conflicting reload values");
    static const unsigned ccr1 = Option::ccr1
        | CombinedOptions<Options...>::ccr1;
    static_assert (Option::ccr1 == 0 || Option::ccr1 == ccr1,
                   "conflicting ccr1 values");
    static const unsigned ccr2 = Option::ccr2
        | CombinedOptions<Options...>::ccr2;
    static_assert (Option::ccr2 == 0 || Option::ccr2 == ccr2,
                   "conflicting ccr2 values");
    static const unsigned ccr3 = Option::ccr3
        | CombinedOptions<Options...>::ccr3;
    static_assert (Option::ccr3 == 0 || Option::ccr3 == ccr3,
                   "conflicting ccr3 values");
    static const unsigned ccr4 = Option::ccr4
        | CombinedOptions<Options...>::ccr4;
    static_assert (Option::ccr4 == 0 || Option::ccr4 == ccr4,
                   "conflicting ccr4 values");
};

} // namespace ucoo

#endif // ucoo_hal_timer_timer_stm32_tcc