aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/SPC5xx/eMIOS_v1/icu_lld.c
blob: a8167c69ce7665a3fd8391826628ce87edca042e (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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
    SPC5 HAL - Copyright (C) 2013 STMicroelectronics

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    eMIOS_v1/icu_lld.c
 * @brief   SPC5xx low level ICU driver code.
 *
 * @addtogroup ICU
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_ICU || defined(__DOXYGEN__)

#include "spc5_emios.h"

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/**
 * @brief   ICUD1 driver identifier.
 * @note    The driver ICUD1 allocates the unified channel eMIOS0_CH0
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH0 || defined(__DOXYGEN__)
ICUDriver ICUD1;
#endif

/**
 * @brief   ICUD2 driver identifier.
 * @note    The driver ICUD2 allocates the unified channel eMIOS0_CH1
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH1 || defined(__DOXYGEN__)
ICUDriver ICUD2;
#endif

/**
 * @brief   ICUD3 driver identifier.
 * @note    The driver ICUD3 allocates the unified channel eMIOS0_CH2
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH2 || defined(__DOXYGEN__)
ICUDriver ICUD3;
#endif

/**
 * @brief   ICUD4 driver identifier.
 * @note    The driver ICUD4 allocates the unified channel eMIOS0_CH3
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH3 || defined(__DOXYGEN__)
ICUDriver ICUD4;
#endif

/**
 * @brief   ICUD5 driver identifier.
 * @note    The driver ICUD5 allocates the unified channel eMIOS0_CH4
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH4 || defined(__DOXYGEN__)
ICUDriver ICUD5;
#endif

/**
 * @brief   ICUD6 driver identifier.
 * @note    The driver ICUD6 allocates the unified channel eMIOS0_CH5
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH5 || defined(__DOXYGEN__)
ICUDriver ICUD6;
#endif

/**
 * @brief   ICUD7 driver identifier.
 * @note    The driver ICUD7 allocates the unified channel eMIOS0_CH6
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH6 || defined(__DOXYGEN__)
ICUDriver ICUD7;
#endif

/**
 * @brief   ICUD8 driver identifier.
 * @note    The driver ICUD8 allocates the unified channel eMIOS0_CH7
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH7 || defined(__DOXYGEN__)
ICUDriver ICUD8;
#endif

/**
 * @brief   ICUD9 driver identifier.
 * @note    The driver ICUD9 allocates the unified channel eMIOS0_CH24
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS0_CH24 || defined(__DOXYGEN__)
ICUDriver ICUD9;
#endif

/**
 * @brief   ICUD10 driver identifier.
 * @note    The driver ICUD10 allocates the unified channel eMIOS1_CH24
 *          when enabled.
 */
#if SPC5_ICU_USE_EMIOS1_CH24 || defined(__DOXYGEN__)
ICUDriver ICUD10;
#endif

/*===========================================================================*/
/* Driver local variables and types.                                         */
/*===========================================================================*/

/**
 * @brief   Width and Period registers.
 */
int16_t width;
int16_t period;

/**
 * @brief   A2 temp registers.
 */
uint16_t A2_1, A2_2, A2_3;

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/**
 * @brief               ICU IRQ handler.
 *
 * @param[in] icup      pointer to the @p ICUDriver object
 */
static void icu_lld_serve_interrupt(ICUDriver *icup) {
  uint32_t gfr = icup->emiosp->GFR.R;

  if (gfr && (1 << icup->ch_number)) {
    uint32_t sr = icup->emiosp->CH[icup->ch_number].CSR.R;

    if(sr && EMIOSS_OVFL && icup->config->overflow_cb != NULL){
      icup->emiosp->CH[icup->ch_number].CSR.R |= EMIOSS_OVFLC;
      _icu_isr_invoke_overflow_cb(icup);
    }
    if (sr && EMIOSS_FLAG){
      icup->emiosp->CH[icup->ch_number].CSR.R |= EMIOSS_FLAGC;
      if (icup->config->mode == ICU_INPUT_ACTIVE_HIGH) {
        if (icup->emiosp->CH[icup->ch_number].CSR.B.UCIN == 1U  &&        \
            icup->config->period_cb != NULL) {
          A2_3 = icup->emiosp->CH[icup->ch_number].CADR.R;
          period = A2_3 - A2_1;
          _icu_isr_invoke_period_cb(icup);
          A2_1 = A2_3;
        } else if (icup->emiosp->CH[icup->ch_number].CSR.B.UCIN == 0 &&   \
            icup->config->width_cb != NULL) {
          A2_2 = icup->emiosp->CH[icup->ch_number].CADR.R;
          width = A2_2 - A2_1;
          _icu_isr_invoke_width_cb(icup);
        }
      } else if (icup->config->mode == ICU_INPUT_ACTIVE_LOW) {
        if (icup->emiosp->CH[icup->ch_number].CSR.B.UCIN == 1U &&         \
            icup->config->width_cb != NULL) {
          A2_2 = icup->emiosp->CH[icup->ch_number].CADR.R;
          width = A2_2 - A2_1;
          _icu_isr_invoke_width_cb(icup);
        } else if (icup->emiosp->CH[icup->ch_number].CSR.B.UCIN == 0 &&   \
            icup->config->period_cb != NULL) {
          A2_3 = icup->emiosp->CH[icup->ch_number].CADR.R;
          period = A2_3 - A2_1;
          _icu_isr_invoke_period_cb(icup);
          A2_1 = A2_3;
        }
      }
    }
    if(sr && EMIOSS_OVR){
      icup->emiosp->CH[icup->ch_number].CSR.R |= EMIOSS_OVRC;
    }

  }
}

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

#if SPC5_ICU_USE_EMIOS0_CH0 || SPC5_ICU_USE_EMIOS0_CH1
#if !defined(SPC5_EMIOS0_GFR_F0F1_HANDLER)
#error "SPC5_EMIOS0_GFR_F0F1_HANDLER not defined"
#endif
/**
 * @brief   eMIOS0 Channels 0 and 1 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS0_GFR_F0F1_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS0_CH0
  icu_lld_serve_interrupt(&ICUD1);
#endif

#if SPC5_ICU_USE_EMIOS0_CH1
  icu_lld_serve_interrupt(&ICUD2);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS0_CH0 || SPC5_ICU_USE_EMIOS0_CH1 */

#if SPC5_ICU_USE_EMIOS0_CH2 || SPC5_ICU_USE_EMIOS0_CH3
#if !defined(SPC5_EMIOS0_GFR_F2F3_HANDLER)
#error "SPC5_EMIOS0_GFR_F2F3_HANDLER not defined"
#endif
/**
 * @brief   eMIOS0 Channels 2 and 3 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS0_GFR_F2F3_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS0_CH2
  icu_lld_serve_interrupt(&ICUD3);
#endif

#if SPC5_ICU_USE_EMIOS0_CH3
  icu_lld_serve_interrupt(&ICUD4);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS0_CH2 || SPC5_ICU_USE_EMIOS0_CH3 */

#if SPC5_ICU_USE_EMIOS0_CH4 || SPC5_ICU_USE_EMIOS0_CH5
#if !defined(SPC5_EMIOS0_GFR_F4F5_HANDLER)
#error "SPC5_EMIOS0_GFR_F4F5_HANDLER not defined"
#endif
/**
 * @brief   eMIOS0 Channels 4 and 5 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS0_GFR_F4F5_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS0_CH4
  icu_lld_serve_interrupt(&ICUD5);
#endif

#if SPC5_ICU_USE_EMIOS0_CH5
  icu_lld_serve_interrupt(&ICUD6);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS0_CH4 || SPC5_ICU_USE_EMIOS0_CH5 */

#if SPC5_ICU_USE_EMIOS0_CH6 || SPC5_ICU_USE_EMIOS0_CH7

#if !defined(SPC5_EMIOS0_GFR_F6F7_HANDLER)
#error "SPC5_EMIOS0_GFR_F6F7_HANDLER not defined"
#endif
/**
 * @brief   eMIOS0 Channels 6 and 7 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS0_GFR_F6F7_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS0_CH6
  icu_lld_serve_interrupt(&ICUD7);
#endif

#if SPC5_ICU_USE_EMIOS0_CH7
  icu_lld_serve_interrupt(&ICUD8);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS0_CH6 || SPC5_ICU_USE_EMIOS0_CH7 */

#if SPC5_ICU_USE_EMIOS0_CH24
#if !defined(SPC5_EMIOS0_GFR_F24F25_HANDLER)
#error "SPC5_EMIOS0_GFR_F24F25_HANDLER not defined"
#endif
/**
 * @brief   eMIOS0 Channels 24 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS0_GFR_F24F25_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS0_CH24
  icu_lld_serve_interrupt(&ICUD9);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS0_CH24 */

#if SPC5_ICU_USE_EMIOS1_CH24
#if !defined(SPC5_EMIOS1_GFR_F24F25_HANDLER)
#error "SPC5_EMIOS1_GFR_F24F25_HANDLER not defined"
#endif
/**
 * @brief   eMIOS1 Channels 24 interrupt handler.
 * @note    It is assumed that the various sources are only activated if the
 *          associated callback pointer is not equal to @p NULL in order to not
 *          perform an extra check in a potentially critical interrupt handler.
 *
 * @isr
 */
CH_IRQ_HANDLER(SPC5_EMIOS1_GFR_F24F25_HANDLER) {

  CH_IRQ_PROLOGUE();

#if SPC5_ICU_USE_EMIOS1_CH24
  icu_lld_serve_interrupt(&ICUD10);
#endif

  CH_IRQ_EPILOGUE();
}
#endif /* SPC5_ICU_USE_EMIOS1_CH24 */

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level ICU driver initialization.
 *
 * @notapi
 */
void icu_lld_init(void) {

  /* Initialize A2 temp registers.*/
  A2_1 = 0U;
  A2_2 = 0U;
  A2_3 = 0U;

  /* eMIOSx channels initially all not in use.*/
  reset_emios0_active_channels();
  reset_emios1_active_channels();

#if SPC5_ICU_USE_EMIOS0_CH0
  /* Driver initialization.*/
  icuObjectInit(&ICUD1);
  ICUD1.emiosp = &EMIOS_0;
  ICUD1.ch_number = 0U;
  ICUD1.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH0 */

#if SPC5_ICU_USE_EMIOS0_CH1
  /* Driver initialization.*/
  icuObjectInit(&ICUD2);
  ICUD2.emiosp = &EMIOS_0;
  ICUD2.ch_number = 1U;
  ICUD2.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH1 */

#if SPC5_ICU_USE_EMIOS0_CH2
  /* Driver initialization.*/
  icuObjectInit(&ICUD3);
  ICUD3.emiosp = &EMIOS_0;
  ICUD3.ch_number = 2U;
  ICUD3.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH2 */

#if SPC5_ICU_USE_EMIOS0_CH3
  /* Driver initialization.*/
  icuObjectInit(&ICUD4);
  ICUD4.emiosp = &EMIOS_0;
  ICUD4.ch_number = 3U;
  ICUD4.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH3 */

#if SPC5_ICU_USE_EMIOS0_CH4
  /* Driver initialization.*/
  icuObjectInit(&ICUD5);
  ICUD5.emiosp = &EMIOS_0;
  ICUD5.ch_number = 4U;
  ICUD5.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH4 */

#if SPC5_ICU_USE_EMIOS0_CH5
  /* Driver initialization.*/
  icuObjectInit(&ICUD6);
  ICUD6.emiosp = &EMIOS_0;
  ICUD6.ch_number = 5U;
  ICUD6.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH5 */

#if SPC5_ICU_USE_EMIOS0_CH6
  /* Driver initialization.*/
  icuObjectInit(&ICUD7);
  ICUD7.emiosp = &EMIOS_0;
  ICUD7.ch_number = 6U;
  ICUD7.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH6 */

#if SPC5_ICU_USE_EMIOS0_CH7
  /* Driver initialization.*/
  icuObjectInit(&ICUD8);
  ICUD8.emiosp = &EMIOS_0;
  ICUD8.ch_number = 7U;
  ICUD8.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH7 */

#if SPC5_ICU_USE_EMIOS0_CH24
  /* Driver initialization.*/
  icuObjectInit(&ICUD9);
  ICUD9.emiosp = &EMIOS_0;
  ICUD9.ch_number = 24U;
  ICUD9.clock = SPC5_EMIOS0_CLK;
#endif /* SPC5_ICU_USE_EMIOS0_CH24 */

#if SPC5_ICU_USE_EMIOS1_CH24
  /* Driver initialization.*/
  icuObjectInit(&ICUD10);
  ICUD10.emiosp = &EMIOS_1;
  ICUD10.ch_number = 24U;
  ICUD10.clock = SPC5_EMIOS1_CLK;
#endif /* SPC5_ICU_USE_EMIOS1_CH24 */

#if SPC5_ICU_USE_EMIOS0

  INTC.PSR[SPC5_EMIOS0_GFR_F0F1_NUMBER].R = SPC5_EMIOS0_GFR_F0F1_PRIORITY;
  INTC.PSR[SPC5_EMIOS0_GFR_F2F3_NUMBER].R = SPC5_EMIOS0_GFR_F2F3_PRIORITY;
  INTC.PSR[SPC5_EMIOS0_GFR_F4F5_NUMBER].R = SPC5_EMIOS0_GFR_F4F5_PRIORITY;
  INTC.PSR[SPC5_EMIOS0_GFR_F6F7_NUMBER].R = SPC5_EMIOS0_GFR_F6F7_PRIORITY;
  INTC.PSR[SPC5_EMIOS0_GFR_F24F25_NUMBER].R = SPC5_EMIOS0_GFR_F24F25_PRIORITY;

#endif

#if SPC5_ICU_USE_EMIOS1

  INTC.PSR[SPC5_EMIOS1_GFR_F24F25_NUMBER].R = SPC5_EMIOS1_GFR_F24F25_PRIORITY;

#endif
}

/**
 * @brief   Configures and activates the ICU peripheral.
 *
 * @param[in] icup      pointer to the @p ICUDriver object
 *
 * @notapi
 */
void icu_lld_start(ICUDriver *icup) {

  //uint32_t emios0_active_channels = get_emios0_active_channels();
  //uint32_t emios1_active_channels = get_emios1_active_channels();

  chDbgAssert(get_emios0_active_channels() < 28, "icu_lld_start(), #1",
                "too many channels");

  chDbgAssert(get_emios1_active_channels() < 28, "icu_lld_start(), #2",
                "too many channels");

  if (icup->state == ICU_STOP) {
    /* Enables the peripheral.*/
#if SPC5_ICU_USE_EMIOS0_CH0
    if (&ICUD1 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH0 */
#if SPC5_ICU_USE_EMIOS0_CH1
    if (&ICUD2 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH1 */
#if SPC5_ICU_USE_EMIOS0_CH2
    if (&ICUD3 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH2 */
#if SPC5_ICU_USE_EMIOS0_CH3
    if (&ICUD4 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH3 */
#if SPC5_ICU_USE_EMIOS0_CH4
    if (&ICUD5 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH4 */
#if SPC5_ICU_USE_EMIOS0_CH5
    if (&ICUD6 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH5 */
#if SPC5_ICU_USE_EMIOS0_CH6
    if (&ICUD7 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH6 */
#if SPC5_ICU_USE_EMIOS0_CH7
    if (&ICUD8 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH7 */
#if SPC5_ICU_USE_EMIOS0_CH24
    if (&ICUD9 == icup)
      increase_emios0_active_channels();
#endif /* SPC5_ICU_USE_EMIOS0_CH24 */
#if SPC5_ICU_USE_EMIOS1_CH24
    if (&ICUD10 == icup)
      increase_emios1_active_channels();
#endif /* SPC5_ICU_USE_EMIOS1_CH24 */

    /* Set eMIOS0 Clock.*/
#if SPC5_ICU_USE_EMIOS0
    active_emios0_clock(icup, NULL);
#endif

    /* Set eMIOS1 Clock.*/
#if SPC5_ICU_USE_EMIOS1
    active_emios1_clock(icup, NULL);
#endif

  }
  /* Configures the peripheral.*/

  /* Channel enables.*/
  icup->emiosp->UCDIS.R &= ~(1 << icup->ch_number);

  /* Clear pending IRQs (if any).*/
  icup->emiosp->CH[icup->ch_number].CSR.R = EMIOSS_OVRC |
      EMIOSS_OVFLC | EMIOSS_FLAGC;

  /* Set clock prescaler and control register.*/
  uint32_t psc = (icup->clock / icup->config->frequency);
  chDbgAssert((psc <= 0xFFFF) &&
              (((psc) * icup->config->frequency) == icup->clock) &&
              ((psc == 1) || (psc == 2) || (psc == 3) || (psc == 4)),
              "icu_lld_start(), #1", "invalid frequency");

  //icup->emiosp->MCR.B.GPREN = 0;
  icup->emiosp->CH[icup->ch_number].CCR.B.UCPEN = 0;
  icup->emiosp->CH[icup->ch_number].CCR.R |=
      EMIOSC_BSL(EMIOS_BSL_INTERNAL_COUNTER) |
      EMIOSC_EDSEL | EMIOS_CCR_MODE_SAIC;
  icup->emiosp->CH[icup->ch_number].CCR.B.UCPRE = psc - 1;
  icup->emiosp->CH[icup->ch_number].CCR.R |= EMIOSC_UCPREN;
  /*
  if (icup->emiosp == &EMIOS_0) {
    icup->emiosp->MCR.R = EMIOSMCR_GPRE(SPC5_EMIOS0_GLOBAL_PRESCALER);
  } else if (icup->emiosp == &EMIOS_1) {
    icup->emiosp->MCR.R = EMIOSMCR_GPRE(SPC5_EMIOS1_GLOBAL_PRESCALER);
  }
  icup->emiosp->MCR.R |= EMIOSMCR_GPREN;

  icup->emiosp->MCR.B.GTBE = 1U;
  */

  /* Set source polarity.*/
  if(icup->config->mode == ICU_INPUT_ACTIVE_HIGH){
    icup->emiosp->CH[icup->ch_number].CCR.R |= EMIOSC_EDPOL;
  } else {
    icup->emiosp->CH[icup->ch_number].CCR.R &= ~EMIOSC_EDPOL;
  }

  /* Direct pointers to the period and width registers in order to make
     reading data faster from within callbacks.*/
  icup->pccrp = &period;
  icup->wccrp = &width;

  /* Channel disables.*/
  icup->emiosp->UCDIS.R |= (1 << icup->ch_number);

}

/**
 * @brief   Deactivates the ICU peripheral.
 *
 * @param[in] icup      pointer to the @p ICUDriver object
 *
 * @notapi
 */
void icu_lld_stop(ICUDriver *icup) {

  //uint32_t emios0_active_channels = get_emios0_active_channels();
  //uint32_t emios1_active_channels = get_emios1_active_channels();

  chDbgAssert(get_emios0_active_channels() < 28, "icu_lld_stop(), #1",
              "too many channels");
  chDbgAssert(get_emios1_active_channels() < 28, "icu_lld_stop(), #2",
              "too many channels");

  if (icup->state == ICU_READY) {

    /* Disables the peripheral.*/
#if SPC5_ICU_USE_EMIOS0_CH0
    if (&ICUD1 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH0 */
#if SPC5_ICU_USE_EMIOS0_CH1
    if (&ICUD2 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH1 */
#if SPC5_ICU_USE_EMIOS0_CH2
    if (&ICUD3 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH2 */
#if SPC5_ICU_USE_EMIOS0_CH3
    if (&ICUD4 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH3 */
#if SPC5_ICU_USE_EMIOS0_CH4
    if (&ICUD5 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH4 */
#if SPC5_ICU_USE_EMIOS0_CH5
    if (&ICUD6 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH5 */
#if SPC5_ICU_USE_EMIOS0_CH6
    if (&ICUD7 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH6 */
#if SPC5_ICU_USE_EMIOS0_CH7
    if (&ICUD8 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH7 */
#if SPC5_ICU_USE_EMIOS0_CH24
    if (&ICUD9 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios0_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS0_CH24 */
#if SPC5_ICU_USE_EMIOS1_CH24
    if (&ICUD10 == icup) {
      /* Reset UC Control Register.*/
      icup->emiosp->CH[icup->ch_number].CCR.R = 0;

      decrease_emios1_active_channels();
    }
#endif /* SPC5_ICU_USE_EMIOS1_CH24 */

    /* eMIOS0 clock deactivation.*/
#if SPC5_ICU_USE_EMIOS0
    deactive_emios0_clock(icup, NULL);
#endif

    /* eMIOS1 clock deactivation.*/
#if SPC5_ICU_USE_EMIOS1
    deactive_emios1_clock(icup, NULL);
#endif
  }
}

/**
 * @brief   Enables the input capture.
 *
 * @param[in] icup      pointer to the @p ICUDriver object
 *
 * @notapi
 */
void icu_lld_enable(ICUDriver *icup) {

  /* Channel enables.*/
  /*
  if (!(icup->emiosp->UCDIS.R && (1 << icup->ch_number))) {

    icup->emiosp->UCDIS.R &= ~(1 << icup->ch_number);
  }
  */

  /* Channel enables.*/
  icup->emiosp->UCDIS.R &= ~(1 << icup->ch_number);

  /* Clear pending IRQs (if any).*/
  icup->emiosp->CH[icup->ch_number].CSR.R = EMIOSS_OVRC |
      EMIOSS_OVFLC | EMIOSS_FLAGC;

  /* Active interrupts.*/
  if (icup->config->period_cb != NULL || icup->config->width_cb != NULL ||  \
      icup->config->overflow_cb != NULL) {
    icup->emiosp->CH[icup->ch_number].CCR.B.FEN = 1U;
  }



  /* Enable Global Time Base.*/
  /*
  if (icup->emiosp->MCR.B.GTBE == 0) {
    icup->emiosp->MCR.B.GTBE = 1U;
  }
  */

}

/**
 * @brief   Disables the input capture.
 *
 * @param[in] icup      pointer to the @p ICUDriver object
 *
 * @notapi
 */
void icu_lld_disable(ICUDriver *icup) {

  /* Clear pending IRQs (if any).*/
  icup->emiosp->CH[icup->ch_number].CSR.R = EMIOSS_OVRC |
        EMIOSS_OVFLC | EMIOSS_FLAGC;

  /* Disable interrupts.*/
  icup->emiosp->CH[icup->ch_number].CCR.B.FEN = 0;

  /* Channel disables.*/
  icup->emiosp->UCDIS.R |= (1 << icup->ch_number);

}

#endif /* HAL_USE_ICU */

/** @} */